|
'我也来练习一下,单字典,,,
Option Explicit
Sub test()
Dim arr, m(4), p(2), i, j, dic
Set dic = CreateObject("scripting.dictionary")
arr = Range("a2:b" & ActiveSheet.UsedRange.Rows.Count).Value
ReDim brr(1 To UBound(arr, 1) * 2, 1 To 2), crr(1 To UBound(brr, 1), 1 To 4)
For i = 1 To UBound(arr, 1)
For j = 1 To UBound(arr, 2)
If Len(arr(i, j)) > 0 Then
If Not dic.exists(arr(i, j)) Then m(0) = m(0) + 1: dic(arr(i, j)) = m(0)
brr(dic(arr(i, j)), j) = arr(i, j)
End If
Next
Next
For i = 1 To m(0)
If Len(brr(i, 1)) > 0 And Len(brr(i, 2)) = 0 Then 'A有B没有
p(1) = 1: p(2) = 1
ElseIf Len(brr(i, 1)) = 0 And Len(brr(i, 2)) > 0 Then 'B有A没有
p(1) = 2: p(2) = 2
Else 'A、B共有
p(1) = 3: p(2) = 1
End If
m(p(1)) = m(p(1)) + 1: crr(m(p(1)), p(1)) = brr(i, p(2))
m(4) = m(4) + 1: crr(m(4), 4) = brr(i, p(2))
Next
[r2].Resize(UBound(crr, 1), 4) = crr
End Sub |
|