[英]Compare two EXCEL worksheets and copy similar data onto a third sheet
[英]Use macro to compare column in two Excel worksheets and add matches to third sheet
我只需要能夠匹配工作表 1 的“A”列中的 ID 編號是否與工作表 2 的相應“A”列匹配,然后將匹配項粘貼到工作表 3 中的“A”列。列范圍在工作表 1 與工作表 2 的范圍不同,工作表 3 需要在找到匹配項時將它們粘貼到每一行中(即,中間沒有空白行)。 我不需要對不匹配的數字做任何事情。
我試圖從不同的線程修改代碼。 它捕獲了一些匹配項,但不是全部。 我猜范圍不正確,但我真的不知道如何在 VBA 中編碼。 另外,列中有一些其他重復項應該張貼的空間。 “if found is nothing then x = 0”是不必要的(甚至可能導致上述錯誤),但如果我把它去掉,我會得到一個類型不匹配錯誤。
Sub matchPAs()
Dim x, i, total, fRow As Integer
Dim found As Range
total = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To total
match1 = Worksheets(1).Range("A" & i).Value
Set found = Sheets(2).Columns("A:A").Find(what:=match1)
If found Is Nothing Then
x = 0
Else
fRow = Sheets(2).Columns("A:A").Find(what:=match1).Row
Worksheets(3).Range("A" & i).Value = Worksheets(1).Range("A" & fRow).Value
End If
Next i
End Sub
邏輯有點不對:
Sub matchPAs()
Dim total As Long
total = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
Dim i As Long
For i = 1 To total
Dim match1 As Variant
match1 = Worksheets(1).Range("A" & i).Value
Dim found As Range
Set found = Sheets(2).Columns("A:A").Find(what:=match1)
If Not found Is Nothing Then
Worksheets(3).Range("A" & Rows.Count).End(xlUp).Offset(1).Value = match1
End If
Next i
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.