|
Sub 数据匹配()
Dim d As Object, dc As Object, dic As Object
Dim ar As Variant, br As Variant
Dim arr1(), arr2(), arr3(), brr()
Set d = CreateObject("scripting.dictionary")
Set dc = CreateObject("scripting.dictionary")
Set dic = CreateObject("scripting.dictionary")
ar = Sheets("系统1数据").[a1].CurrentRegion
br = Sheets("系统2数据").[a1].CurrentRegion
ReDim arr1(1 To UBound(ar) + UBound(br), 1 To 3) ''两表均有
ReDim arr2(1 To UBound(ar) + UBound(br), 1 To 3) ''2有1没有
ReDim arr3(1 To UBound(ar) + UBound(br), 1 To 3) ''1有2没有
For i = 2 To UBound(ar)
If Trim(ar(i, 1)) <> "" Then
d(Trim(ar(i, 1))) = i
End If
Next i
For i = 2 To UBound(br)
If Trim(br(i, 1)) <> "" Then
dc(Trim(br(i, 1))) = i
If d.Exists(Trim(br(i, 1))) Then
n_1 = n_1 + 1
For j = 1 To 3
arr1(n_1, j) = br(i, j)
Next j
ElseIf Not d.Exists(Trim(br(i, 1))) Then
n_2 = n_2 + 1
For j = 1 To 3
arr2(n_2, j) = br(i, j)
Next j
End If
End If
Next i
For i = 2 To UBound(ar)
If Trim(ar(i, 1)) <> "" Then
If dc.Exists(Trim(br(i, 1))) Then
n_1 = n_1 + 1
For j = 1 To 3
arr1(n_1, j) = ar(i, j)
Next j
ElseIf Not dc.Exists(Trim(br(i, 1))) Then
n_3 = n_3 + 1
For j = 1 To 3
arr3(n_3, j) = ar(i, j)
Next j
End If
End If
Next i
ReDim brr(1 To n_1, 1 To 3)
For i = 1 To n_1
t = dic(Trim(arr1(i, 1)))
If t = "" Then
k = k + 1
dic(Trim(arr1(i, 1))) = k
t = k
For j = 1 To 3
brr(k, j) = arr1(i, j)
Next j
End If
Next i
With Sheets("匹配成功的数据")
.[a1].CurrentRegion.Offset(1) = Empty
If n_1 <> "" Then .[a2].Resize(n_1, 3) = brr
End With
With Sheets("匹配失败的数据1")
.[a1].CurrentRegion.Offset(1) = Empty
If n_3 <> "" Then .[a2].Resize(n_3, 3) = arr3
End With
With Sheets("匹配失败的数据2")
.[a1].CurrentRegion.Offset(1) = Empty
If n_2 <> "" Then .[a2].Resize(n_2, 3) = arr2
End With
MsgBox "ok!"
End Sub
|
|