我有兩個工作簿(或2張):工作簿A和B.工作簿我 要比較: 列B和C在工作簿中的WITH 工作簿B中的列A和B 如果找到匹配然後 我需要複製工作簿B中的MATCHED行並將其粘貼到工作簿A上的MATCHED行。換句話說:我需要複製單元格的值工作簿B的匹配行的列C和D到工作簿A中匹配行的列D和E的單元格上。比較在2個工作簿2列,複製匹配的行,如果發現匹配
我到目前爲止的內容只有比較我希望是正確的2列。 下面的代碼爲2張,而不是兩個工作簿:
Sub compareNcopy()
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
Set sh1 = Sheets("Sheet1"): Set sh2 = Sheets(2): Set sh3 = Sheets(3)
Dim i As Long, j As Long,
Dim lr1 As Long, lr2 As Long
Dim nxtRow As Long
Dim rng1 As Range, rng2 As Range, rng3 As Range
lr1 = sh1.Range("A" & Rows.Count).End(xlUp).Row
lr2 = sh2.Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lr1
Set rng1 = sh1.Range("A" & i)
For j = 1 To lr2
Set rng2 = sh2.Range("A" & j)
If StrComp(CStr(rng1.Value), CStr(rng2.Value), vbTextCompare) = 0 Then
If rng1.Offset(0, 1).Value = rng2.Offset(0, 1).Value Then
End If
End If
Set rng2 = Nothing
Next j
Set rng1 = Nothing
Next i
End Sub
幫助將不勝感激。
感謝您的回覆。我添加了你建議的行,但它給了我一個「不匹配的運行時錯誤:13」的任何想法,爲什麼?我更改了代碼以比較列: 表(sh2).Cells(j,3).Resize(1,2).Copy Destination:= Sheets(sh1).Cells(i,4).Resize(1, 2) – user3720702
@ user3720702現在已修復,對此感到遺憾。 – JRLambert
工作!謝謝! – user3720702