在工作表 2 中,A 列中有一组规则。
列 A 中的示例每行中有多个代码,行 B 到 H 具有基于对应于该代码的数据。
在工作表 1 中,如果此代码与 A 列中的代码匹配,我希望能够放置其中一个代码并让 VBA 从工作表 2 传输 B:H 行。
这是我到目前为止的程序,它传输行,但不是正确的行。
Dim i As Integer
Dim x As Integer
Dim row As Integer
Dim oldRow As Integer
Dim found As Boolean
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
Dim rng As Range, cell As Range, rng2 As Range, cell2 As Range
Set rng2 = ws2.Range("A1:A212")
Set rng = ws1.Range("A1:A212")
row = 1
oldRow = 1
For Each cell In rng
row = row + 1
For Each cell2 In rng2
oldRow = oldRow + 1
If cell.Value = cell2.Value Then
row = row - 1
ws1.Cells(row, 2) = ws2.Cells(oldRow, 2)
ws1.Cells(row, 3) = ws2.Cells(oldRow, 3)
ws1.Cells(row, 4) = ws2.Cells(oldRow, 4)
ws1.Cells(row, 5) = ws2.Cells(oldRow, 5)
ws1.Cells(row, 6) = ws2.Cells(oldRow, 6)
ws1.Cells(row, 7) = ws2.Cells(oldRow, 7)
ws1.Cells(row, 8) = ws2.Cells(oldRow, 8)
found = True
End If
Next
found = False
oldRow = 1
Next
End Sub
感谢您的帮助,谢谢。