代碼:
Sub Ck()
Set d = CreateObject("Scripting.Dictionary")
arr1 = Range([A2], [A65536].End(xlUp))
arr2 = Range([B2], [B65536].End(xlUp))
For i = 1 To UBound(arr1)
d(arr1(i, 1)) = 0
Next
For j = 1 To UBound(arr2)
If d.exists(arr2(j, 1)) Then d(arr2(j, 1)) = 1
Next
For Each d1 In d.keys
If d(d1) = 0 Then d.Remove (d1)
Next
Range("C2").Resize(d.Count, 1) = WorksheetFunction.Transpose(d.keys)
End Sub
結果如圖:代碼解析:
arr1 = Range([A2], [A65536].End(xlUp)) ’建立數組,将A列相應的值放入數組放入arr1
arr2 = Range([B2], [B65536].End(xlUp)) ’建立數組,将B列相應的值放入數組放入arr2
For i = 1 To UBound(arr1)
d(arr1(i, 1)) = 0 ’将arr1的值寫入字典的key,其item值等于0
Next
For j = 1 To UBound(arr2)
If d.exists(arr2(j, 1)) Then d(arr2(j, 1)) = 1 ’如有B列的數值等于字典的key,那麼,将其對應key的item賦值為1
Next
For Each d1 In d.keys
If d(d1) = 0 Then d.Remove (d1) ‘删除字典中item為0的值,剩餘的為item為1的值,也就是獲得了兩列的重複項
Range("C2").Resize(d.Count, 1) = WorksheetFunction.Transpose(d.keys)
‘轉置,在C列列出重複值。
,
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!