我搜索了堆栈溢出的类似问题,这里是我找到的代码(略有修改),下面的代码匹配1列。
要求在两个工作表中匹配2列(名字和姓氏)而不是1列第一名称,并且在4.mobile工作表中从第18行开始并忽略第18行上方的任何匹配名称
Sub CopyBasedonSheet1()
Dim i As Long
Dim j As Long
Sheet4LastRow = Worksheets("4. Mobile").Range("D" & Rows.Count).End(xlUp).Row
sheet7LastRow = Worksheets("Contacts").Range("A" & Rows.Count).End(xlUp).Row
For j = 1 To Sheet4LastRow
For i = 1 To sheet7LastRow
If Worksheets("4. Mobile").Cells(j, 4).Value = Worksheets("Contacts").Cells(i, 1).Value Then
Worksheets("4. Mobile").Cells(j, 1).Value = Worksheets("Contacts").Cells(i, 3).Value
Worksheets("4. Mobile").Cells(j, 3).Value = Worksheets("Contacts").Cells(i, 4).Value
Worksheets("4. Mobile").Cells(j, 6).Value = Worksheets("Contacts").Cells(i, 5).Value
Worksheets("4. Mobile").Cells(j, 7).Value = Worksheets("Contacts").Cells(i, 6).Value
Worksheets("4. Mobile").Cells(j, 8).Value = Worksheets("Contacts").Cells(i, 7).Value
Else
End If
Next i
Next j
End Sub
答案 0 :(得分:0)
如果我了解您需要做的只是在If:
中添加And
Sub CopyBasedonSheet1()
Dim i As Long
Dim j As Long
Sheet4LastRow = Worksheets("4. Mobile").Range("D" & Rows.Count).End(xlUp).Row
sheet7LastRow = Worksheets("Contacts").Range("A" & Rows.Count).End(xlUp).Row
For j = 1 To Sheet4LastRow
For i = 1 To sheet7LastRow
If Worksheets("4. Mobile").Cells(j, 4).Value = Worksheets("Contacts").Cells(i, 1).Value _
And Worksheets("4. Mobile").Cells(j, 5).Value = Worksheets("Contacts").Cells(i, 2).Value Then
Worksheets("4. Mobile").Cells(j, 1).Value = Worksheets("Contacts").Cells(i, 3).Value
Worksheets("4. Mobile").Cells(j, 3).Value = Worksheets("Contacts").Cells(i, 4).Value
Worksheets("4. Mobile").Cells(j, 6).Value = Worksheets("Contacts").Cells(i, 5).Value
Worksheets("4. Mobile").Cells(j, 7).Value = Worksheets("Contacts").Cells(i, 6).Value
Worksheets("4. Mobile").Cells(j, 8).Value = Worksheets("Contacts").Cells(i, 7).Value
Else
End If
Next i
Next j
End Sub