VBA两列数据对⽐查相同与不同(字典与数组学习笔记)Sub compare()
Dim rowt1%, rowt2%, i%, k%, a%, b%, c%
Dim arr, arr1, arr2
Dim d1 As Object
Dim d2 As Object
Set d1 = CreateObject(“scripting.dictionary”)
Set d2 = CreateObject(“scripting.dictionary”)
rowt1 = Cells(1048576, 1).End(3).Row
rowt2 = Cells(1048576, 2).End(3).Row
Range(Cells(2, 1), Cells(rowt1, 1)).ClearFormats
Range(Cells(2, 2), Cells(rowt1, 2)).ClearFormats
a = 1
b = 1
c = 1
If rowt1 >= rowt2 Then
i = rowt1
Else
vba自学好学吗i = rowt2
End If
On Error Resume Next
ReDim arr(1 To i, 1 To 5)
arr = Range(Cells(2, 1), Cells(i, 5))
arr1 = Range(Cells(2, 1), Cells(rowt1, 1))
arr2 = Range(Cells(2, 2), Cells(rowt2, 2))
For k = 1 To UBound(arr1)
d1.Add Trim(arr1(k, 1)), “”
Next k
For k = 1 To UBound(arr2)
d2.Add Trim(arr2(k, 1)), “”
Next k
For k = 1 To UBound(arr1)
ists(Trim(arr1(k, 1))) Then
arr(a, 3) = arr1(k, 1)
a = a + 1
Else
arr(c, 5) = arr1(k, 1)
c = c + 1
End If
Next k
For k = 1 To UBound(arr2)
ists(Trim(arr2(k, 1))) Then Else
arr(b, 4) = arr2(k, 1)
b = b + 1
End If
Next k
Range(Cells(2, 1), Cells(i, 5)) = arr
Set arr = Nothing
Set arr1 = Nothing
Set arr2 = Nothing
Set d1 = Nothing
Set d2 = Nothing
End Sub

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。