如果列包含VBA中另一列的任何值,则复制整行

时间:2017-04-14 17:13:23

标签: vba copy row paste

我是VBA的新手。 我有3张:'星期日','coords'和'过滤'。 我想检查工作表'Sunday'的'A'列是否等于'coords'表中'J'列中的任何值。 如果为TRUE - 复制“已过滤”表格中的行。

到目前为止,我尝试了以下代码:

Sub CopyRow()

Dim lastRow As Long
Dim sheetName1 As String
Dim sheetName2 As String
Dim sheetName3 As String

    sheetName1 = "Sunday"            'Insert your sheet name here
    sheetName2 = "coords"
    sheetName3 = "filtered"
    lastRow = Sheets(sheetName1).Range("A" & Rows.Count).End(xlUp).Row

    For lRow = 2 To lastRow         'Loop through all rows

        If Sheets(sheetName1).Cells(lRow, "A") = Sheets(sheetName2).Cells(lRow, "J") Then
            c.EntireRow.Copy Worksheets(sheetName3).Range("A" & Rows.Count).End(xlUp).Offset(1)
        End If

    Next lRow

End Sub

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

如果要检查列J任意行中是否存在值,请尝试以下操作:

If Application.CountIf(Sheets(sheetName2).Columns("J"), Sheets(sheetName1).Cells(lRow, "A").Value2) > 0 Then
     Sheets(sheetName3).Range("A" & Rows.count).End(xlUp).offset(1).EntireRow.Value = Sheets(sheetName1).Rows(lRow).Value2
End If
相关问题