这就是我想做的事情:
换句话说:
我想要的宏应匹配并复制A和B的整个工作表。工作表A中的数据只能使用一次。
到目前为止,这是我所拥有的,但它似乎不起作用。
Dim sh1 As Worksheet, sh2 As Worksheet
Dim j As Long, i As Long, lastrow As Long
Set sh1 = Worksheets("Worksheet A")
Set sh2 = Worksheets("Worksheet B")
lastrow = sh1.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastrow
j = (i - 2) * 4 + 1
If sh1.Cells(i, "H").Value = sh2.Cells(j, "E").Value And _
sh1.Cells(i, "J").Value = sh2.Cells(j, "H").Value And _
sh1.Cells(i, "K").Value = sh2.Cells(j, "I").Value Then
sh1.Cells(i, "O").Copy sh2.Cells(j, "L")
End If
j = j + 4
Next
答案 0 :(得分:2)
更新您需要两个循环来完成您的工作。这个新的子程序适用于任何行。请注意多个匹配,因为它只需要最后一个匹配:
Sub CopyCells()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim j As Long, i As Long, lastrow1 As Long, lastrow2 As Long
Set sh1 = Worksheets("Worksheet A")
Set sh2 = Worksheets("Worksheet B")
lastrow1 = sh1.Cells(Rows.Count, "A").End(xlUp).Row
lastrow2 = sh2.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastrow1
For j = 1 To lastrow2
If sh1.Cells(i, "H").Value = sh2.Cells(j, "E").Value And _
sh1.Cells(i, "J").Value = sh2.Cells(j, "H").Value And _
sh1.Cells(i, "K").Value = sh2.Cells(j, "I").Value Then
sh1.Cells(i, "L").Value = sh2.Cells(j, "O").Value
End If
Next j
Next i
End Sub