|
Sub ExtractDuplicates()
Dim wsMatch As Worksheet
Dim wsData As Worksheet
Dim wsResult As Worksheet
Dim lastRowMatch As Long
Dim lastRowData As Long
Dim i As Long
Dim j As Long
Dim dict As Object
Dim arrMatch() As Variant
Dim arrData() As Variant
Dim arrResult() As Variant
Dim count As Long
Set dict = CreateObject("Scripting.Dictionary")
Set wsMatch = ThisWorkbook.Sheets("待匹配")
Set wsData = ThisWorkbook.Sheets("总数据")
Set wsResult = ThisWorkbook.Sheets.Add
wsResult.Name = "结果"
lastRowMatch = wsMatch.Cells(wsMatch.Rows.Count, "A").End(xlUp).Row
lastRowData = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
arrMatch = wsMatch.Range("A1:H" & lastRowMatch).Value
arrData = wsData.Range("A1:H" & lastRowData).Value
ReDim arrResult(1 To lastRowMatch + lastRowData, 1 To 8)
count = 1
For i = 1 To lastRowMatch
If dict.Exists(arrMatch(i, 1)) Then
For j = dict(arrMatch(i, 1)) To i
arrResult(count, 1) = arrMatch(j, 1)
arrResult(count, 2) = arrMatch(j, 2)
arrResult(count, 3) = arrMatch(j, 3)
arrResult(count, 4) = arrMatch(j, 4)
arrResult(count, 5) = arrMatch(j, 5)
arrResult(count, 6) = arrMatch(j, 6)
arrResult(count, 7) = arrMatch(j, 7)
arrResult(count, 8) = arrMatch(j, 8)
count = count + 1
Next j
End If
dict(arrMatch(i, 1)) = i
Next i
For i = 1 To lastRowData
If dict.Exists(arrData(i, 1)) Then
For j = dict(arrData(i, 1)) To lastRowMatch
If arrMatch(j, 1) = arrData(i, 1) Then
arrResult(count, 1) = arrData(i, 1)
arrResult(count, 2) = arrData(i, 2)
arrResult(count, 3) = arrData(i, 3)
arrResult(count, 4) = arrData(i, 4)
arrResult(count, 5) = arrData(i, 5)
arrResult(count, 6) = arrData(i, 6)
arrResult(count, 7) = = arrData(i, 7)
arrResult(count, 8) = arrData(i, 8)
count = count + 1
End If
Next j
End If
Next i
wsResult.Range("A1:H" & count - 1).Value = arrResult |
评分
-
1
查看全部评分
-
|