通过应用多个条件将数据从一个工作表获取到另一个工作表

时间:2017-11-21 16:51:51

标签: vba excel-vba excel

我对这个VBA编码很新。有人可以帮我这个。 我在Sheet1中有一些数据,我想将数据复制到Sheet2,条件如下。 如果“K”=“是”则L,M,N,O,P应该在下一行。以相同的方式,如果“Q”=“是”,那么R,S,T,U,V应该在下一行。以下是示例数据enter image description here

预期产量: enter image description here

1 个答案:

答案 0 :(得分:0)

以下代码可能会满足您的要求:

Sub MyMacro()
LastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
    emptyrow = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row + 1
    Sheet2.Cells(emptyrow, 1).Value = Sheet1.Cells(i, 1).Value
    Sheet2.Cells(emptyrow, 2).Value = Sheet1.Cells(i, 2).Value
    Sheet2.Cells(emptyrow, 3).Value = Sheet1.Cells(i, 3).Value
    Sheet2.Cells(emptyrow, 4).Value = Sheet1.Cells(i, 4).Value
    Sheet2.Cells(emptyrow, 5).Value = Sheet1.Cells(i, 5).Value
    Sheet2.Cells(emptyrow, 6).Value = Sheet1.Cells(i, 6).Value
    Sheet2.Cells(emptyrow, 7).Value = Sheet1.Cells(i, 7).Value
    Sheet2.Cells(emptyrow, 8).Value = Sheet1.Cells(i, 8).Value
    Sheet2.Cells(emptyrow, 9).Value = Sheet1.Cells(i, 9).Value
    Sheet2.Cells(emptyrow, 10).Value = Sheet1.Cells(i, 10).Value
Check = UCase(Sheet1.Cells(i, 11).Value)
    If Check = "YES" Then
        emptyrow = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row + 1
        Sheet2.Cells(emptyrow, 1).Value = Sheet1.Cells(i, 1).Value
        Sheet2.Cells(emptyrow, 2).Value = Sheet1.Cells(i, 2).Value
        Sheet2.Cells(emptyrow, 3).Value = Sheet1.Cells(i, 3).Value
        Sheet2.Cells(emptyrow, 4).Value = Sheet1.Cells(i, 4).Value
        Sheet2.Cells(emptyrow, 5).Value = Sheet1.Cells(i, 5).Value
        Sheet2.Cells(emptyrow, 6).Value = Sheet1.Cells(i, 12).Value
        Sheet2.Cells(emptyrow, 7).Value = Sheet1.Cells(i, 13).Value
        Sheet2.Cells(emptyrow, 8).Value = Sheet1.Cells(i, 14).Value
        Sheet2.Cells(emptyrow, 9).Value = Sheet1.Cells(i, 15).Value
        Sheet2.Cells(emptyrow, 10).Value = Sheet1.Cells(i, 16).Value
    End If
Check = UCase(Sheet1.Cells(i, 17).Value)
    If Check = "YES" Then
        emptyrow = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row + 1
        Sheet2.Cells(emptyrow, 1).Value = Sheet1.Cells(i, 1).Value
        Sheet2.Cells(emptyrow, 2).Value = Sheet1.Cells(i, 2).Value
        Sheet2.Cells(emptyrow, 3).Value = Sheet1.Cells(i, 3).Value
        Sheet2.Cells(emptyrow, 4).Value = Sheet1.Cells(i, 4).Value
        Sheet2.Cells(emptyrow, 5).Value = Sheet1.Cells(i, 5).Value
        Sheet2.Cells(emptyrow, 6).Value = Sheet1.Cells(i, 18).Value
        Sheet2.Cells(emptyrow, 7).Value = Sheet1.Cells(i, 19).Value
        Sheet2.Cells(emptyrow, 8).Value = Sheet1.Cells(i, 20).Value
        Sheet2.Cells(emptyrow, 9).Value = Sheet1.Cells(i, 21).Value
        Sheet2.Cells(emptyrow, 10).Value = Sheet1.Cells(i, 22).Value
    End If
Next i
End Sub

Sheet2上的输出如下所示:

Team    Team    Team    Member      Team Member
Jame    Rich    11      One of My   Great Teacher
Tar     Scott   98      Rockstar    Rockstar
Ash     Mit     94      good team   Great!
Kate    Wilde   1       One of my   One of my