我附上了一张图片,显示了我的数据和我想要实现的目标。
我的代码不能正常工作。它复制 14 和 bb1 的数据,然后卡在循环中。请帮我纠正。
为什么我要使用 find 方法是,在前的 H 行中,我将在 1000 行中匹配大约 20 到 30 个 id ..所以循环一整行需要很长时间。
任何想法如何更正和优化代码。谢谢
Sub Findandcopy()
Dim shtOld As Worksheet, shtNew As Worksheet
Dim oldRow As Integer
Dim newRow As Integer
Dim i As Integer, id, f As Range
Dim g As Range
Dim currow As Long
Set shtOld = ThisWorkbook.Sheets("Sheet1")
Set shtNew = ThisWorkbook.Sheets("Sheet2")
With shtOld.Range("H1:H30")
Set c = .find("*")
If Not c Is Nothing Then
firstAddress = c.Address
Do
Set f = shtNew.Range("G2:G40").find(c)
If Not f Is Nothing Then
currow = f.Cells.Row
shtNew.Activate
Set g = shtNew.Range("G" & currow).Resize(4, 2)
g.Copy
shtOld.Activate
shtOld.Range("I" & c.Row).Select
ActiveSheet.Paste
End If
Set c = .FindNext("*")
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
End Sub