简体   繁体   English

VBA代码可根据条件复制特定列中的单元格并将其粘贴(值)到另一张工作表中特定列中的单元格

[英]VBA code to copy cells in specific column and paste (value) it to cells in specific column on another Sheet based on conditions

VBA noob here, I need to copy from specific column in Sheet1 and pastevalue it to specific column in Sheet2 based on 2 conditions. VBA新手,我需要根据2种情况从Sheet1中的特定列复制并将其粘贴值到Sheet2中的特定列。 Details (it's not a code): 详细信息(不是代码):

i=2 & j=4

IF Sheet1.Cells(i, 4)<>0 and Sheet1.Cells(i, 49)<>0, Type "FK" in _
Sheet2.Cells(j, 1), Type "OF" in Sheet2.Cells(j + 1, 1), copy the following

Copy from **Sheet1**      Paste in **Sheet2**
Cells(i, 52)              Cells(j, 2)
Cells(i, 51)              Cells(j, 3)
Cells(i, 4)               Cells(j, 4)
Cells(i, 1)               Cells(j, 5)
Cells(i, 53)              Cells(j, 6)
Cells(i, 7)               Cells(j, 7)
Cells(i, 10)              Cells(j, 8)
Cells(i, 5)               Cells(j, 9)
Cells(i, 6)               Cells(j, 10)
Cells(i, 49)              Cells(j, 11)
Cells(i, 50)              Cells(j, 12)
Cells(i, 51)              Cells(j, 13)
Cells(i, 51)              Cells(j, 15)
Cells(i, 51)              Cells(j, 16)
Cells(i, 51)              Cells(j, 17)
Cells(i, 19)              Cells(j + 1, 2)
Cells(i, 20)              Cells(j + 1, 3)
Cells(i, 44)              Cells(j + 1, 4)
Cells(i, 28)              Cells(j + 1, 5)
Cells(i, 30)              Cells(j + 1, 6)
Cells(i, 37)              Cells(j + 1, 7)
Cells(i, 41)              Cells(j + 1, 8)
Cells(i, 46)              Cells(j + 1, 9)
Cells(i, 51)              Cells(j + 1, 10)
Cells(i, 51)              Cells(j + 1, 11)

next i

IF Sheet1.Cells(i, 4)<>0 and Sheet1.Cells(i, 49)=0, Type "OF" in _
Sheet2.Cells(j, 1), copy the following

Copy from **Sheet1**    Paste in **Sheet2**
Cells(i, 19)            Cells(j, 2)
Cells(i, 20)            Cells(j, 3)
Cells(i, 44)            Cells(j, 4)
Cells(i, 28)            Cells(j, 5)
Cells(i, 30)            Cells(j, 6)
Cells(i, 37)            Cells(j, 7)
Cells(i, 41)            Cells(j, 8)
Cells(i, 46)            Cells(j, 9)
Cells(i, 51)            Cells(j, 10)
Cells(i, 51)            Cells(j, 11)

next i

Else, next i (start again for i + 1)

So, basically what I'm trying to do is, if condition 1 is true I will copy data from 1 row and paste it into the next 2 empty rows in another sheet. 因此,基本上我想做的是,如果条件1为true,我将复制1行中的数据并将其粘贴到另一张表的下2个空行中。 Meanwhile, if condition 2 is true, copy data from 1 row and paste it into the next empty row in another sheet. 同时,如果条件2为真,则从1行复制数据并将其粘贴到另一张纸的下一个空行中。 Sorry for the long post, don't know any other way to explain it. 很长的帖子,很抱歉,不知道其他解释方法。 Thank you all before. 谢谢大家

Is this doing what you want: 这是在做什么吗?

    Sub copyABunchOfThings()

    Dim i As Integer, j As Integer
    i = 2
    j = 4

    For i = 1 To 10 'Change 1 and 10 into your numbers.

    If Sheet1.Cells(i, 4) <> 0 And Sheet1.Cells(i, 49) <> 0 Then
        Sheet2.Cells(j, 1).Value = "FK"
        Sheet2.Cells(j + 1, 1).Value = "OF"

        'Put in **Sheet 2**   what is in **Sheet 1**
        Sheet2.Cells(j, 2) = Sheet1.Cells(i, 52)
        Sheet2.Cells(j, 3) = Sheet1.Cells(i, 51)
        Sheet2.Cells(j, 4) = Sheet1.Cells(i, 4)
        Sheet2.Cells(j, 5) = Sheet1.Cells(i, 1)
        Sheet2.Cells(j, 6) = Sheet1.Cells(i, 53)
        Sheet2.Cells(j, 7) = Sheet1.Cells(i, 7)
        Sheet2.Cells(j, 8) = Sheet1.Cells(i, 10)
        Sheet2.Cells(j, 9) = Sheet1.Cells(i, 5)
        Sheet2.Cells(j, 10) = Sheet1.Cells(i, 6)
        Sheet2.Cells(j, 11) = Sheet1.Cells(i, 49)
        Sheet2.Cells(j, 12) = Sheet1.Cells(i, 50)
        Sheet2.Cells(j, 13) = Sheet1.Cells(i, 51)
        Sheet2.Cells(j, 15) = Sheet1.Cells(i, 51)
        Sheet2.Cells(j, 16) = Sheet1.Cells(i, 51)
        Sheet2.Cells(j, 17) = Sheet1.Cells(i, 51)
        Sheet2.Cells(j + 1, 2) = Sheet1.Cells(i, 19)
        Sheet2.Cells(j + 1, 3) = Sheet1.Cells(i, 20)
        Sheet2.Cells(j + 1, 4) = Sheet1.Cells(i, 44)
        Sheet2.Cells(j + 1, 5) = Sheet1.Cells(i, 28)
        Sheet2.Cells(j + 1, 6) = Sheet1.Cells(i, 30)
        Sheet2.Cells(j + 1, 7) = Sheet1.Cells(i, 37)
        Sheet2.Cells(j + 1, 8) = Sheet1.Cells(i, 41)
        Sheet2.Cells(j + 1, 9) = Sheet1.Cells(i, 46)
        Sheet2.Cells(j + 1, 10) = Sheet1.Cells(i, 51)
        Sheet2.Cells(j + 1, 11) = Sheet1.Cells(i, 51)

    ElseIf Sheet1.Cells(i, 4) <> 0 And Sheet1.Cells(i, 49) = 0 Then
        Sheet2.Cells(j, 1) = "OF"

        'Put in **Sheet 2**   what is in **Sheet 1**
        Sheet2.Cells(j, 2) = Sheet1.Cells(i, 19)
        Sheet2.Cells(j, 3) = Sheet1.Cells(i, 20)
        Sheet2.Cells(j, 4) = Sheet1.Cells(i, 44)
        Sheet2.Cells(j, 5) = Sheet1.Cells(i, 28)
        Sheet2.Cells(j, 6) = Sheet1.Cells(i, 30)
        Sheet2.Cells(j, 7) = Sheet1.Cells(i, 37)
        Sheet2.Cells(j, 8) = Sheet1.Cells(i, 41)
        Sheet2.Cells(j, 9) = Sheet1.Cells(i, 46)
        Sheet2.Cells(j, 10) = Sheet1.Cells(i, 51)
        Sheet2.Cells(j, 11) = Sheet1.Cells(i, 51)
    End If

    Next i

    End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据列中的文本将单元格值复制并粘贴到特定的单元格 - Copy and paste cell value to specific cells based on text in column 代码不会将特定的单元格复制并粘贴到新工作表VBA中 - Code Won't Copy and Paste Specific Cells Into New Sheet VBA 根据第一张工作表中列的非空单元格,将特定的行范围复制到另一张工作表的特定范围 - Copy specific row range to another sheet's specific range based on non empty cells of a column in first sheet VBA循环复制和粘贴行基于一个特定的单元格值 - VBA loop copy and paste rows based one a specific cells value 根据列标题将单元格粘贴到特定行中 - Paste cells in specific row based on column header Vba代码比较具有包含日期​​的单元格的列并将特定文本粘贴到另一列 - Vba code to compare columns that have cells containing dates and paste specific text to another column vba代码以查找特定值并在该列的后续单元格中复制值并粘贴到新工作表中 - vba code to find a specific value and copy value in subsequent cell in the column and paste in a new sheet 根据条件将单元格从特定列复制到另一个工作表 - Copy cells from a specific column to another worksheet based on criteria 将特定的单元格复制到另一个工作表 - Copy specific cells to another sheet 如何根据工作表2中单元格的值从工作表1中复制特定单元格并将其粘贴到工作表2的相应行中? - How do I copy specific cells from sheet 1 and paste into corresponding rows of sheet 2 , based on values of cells in sheet 2?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM