2017-04-14 55 views
0

我是VBA中的新成員。 我有3張紙:'星期天','coords'和'過濾'。 我想檢查'Sunday'表中的'A'列是否與'coords'表的'J'列中的任何值相等。 如果爲TRUE - 複製'過濾'表中的行。如果該列包含VBA中另一列的任何值,則複製整行

到目前爲止,我已經嘗試下面的代碼:

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 

任何幫助是極大的讚賞

+0

到目前爲止您嘗試了什麼? – sktneer

+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 
相關問題