我在A列中有2000个数字,其中一些是重复的,我需要将匹配的数字从B列中给出的可能数字列表中复制到另一张纸中。这些数字只是一个例子:
Column A Column B
122 170
134 289
170 300
170 428
289 584
300 817
300
428
438
584
622
622
817
答案 0 :(得分:0)
如果您只需要复制A和B列中匹配的行,则可以尝试:
Sub Compare()
Dim rowA As Integer
Dim rowB As Integer
Dim lastRowA As Integer
Dim lastRowB As Integer
Dim spot As Integer: spot = 1
With ThisWorkbook
lastRowA = Sheets(1).Cells(Sheets(1).Rows.Count, "A").End(xlUp).row
lastRowB = Sheets(1).Cells(Sheets(1).Rows.Count, "B").End(xlUp).row
.Worksheets(2).Range("A" & spot).Value = "Col A Value"
.Worksheets(2).Range("B" & spot).Value = "Col B Value"
.Worksheets(2).Range("C" & spot).Value = "Col C Value"
.Worksheets(2).Range("D" & spot).Value = "Col D Value"
.Worksheets(2).Range("E" & spot).Value = "Col E Value"
.Worksheets(2).Range("F" & spot).Value = "Row found on Col A"
.Worksheets(2).Range("G" & spot).Value = "Row found on Col B"
spot = 2
For rowA = 1 to lastRowA
For rowB = 1 to lastRowB
If .Worksheets(1).Range("A" & rowA).Value = .Worksheets(1).Range("B" & rowB).Value Then
.Worksheets(2).Range("A" & spot).Value = .Worksheets(1).Range("A" & rowA).Value
.Worksheets(2).Range("B" & spot).Value = .Worksheets(1).Range("B" & rowB).Value
.Worksheets(2).Range("C" & spot).Value = .Worksheets(1).Range("C" & rowB).Value
.Worksheets(2).Range("D" & spot).Value = .Worksheets(1).Range("D" & rowB).Value
.Worksheets(2).Range("E" & spot).Value = .Worksheets(1).Range("E" & rowB).Value
.Worksheets(2).Range("F" & spot).Value = rowA
.Worksheets(2).Range("G" & spot).Value = rowB
spot = spot + 1
End If
Next
Next
End With
End Sub
如果您的原始值在工作表1中,则会将匹配的值复制到工作表2中。...这只是一个想法,仅通过查看帖子并不确定目标是什么完全
编辑: 在C,D和E列中添加了数据……将“找到的”信息推到最后