Option Explicit
Sub test()
Dim arr, i, j, k, dic(1), t, m, n, key, flag As Boolean
For i = 0 To UBound(dic)
Set dic(i) = CreateObject("scripting.dictionary")
Next
arr = Split("4,5,9", ",")
For i = 0 To UBound(arr)
dic(0)(arr(i)) = 1
Next
arr = Range("b5:b" & [b5].End(xlDown).Row).Value
If UBound(arr, 1) Mod 2 > 0 Then MsgBox "!": Exit Sub
ReDim brr(1 To UBound(arr, 1) / 2, 1 To dic(0).Count)
For i = 1 To UBound(arr, 1) Step 2
For j = i To i + 1
t = Split(arr(j, 1))
For k = 0 To UBound(t)
If dic(0).exists(t(k)) Then dic(1)(t(k)) = dic(1)(t(k)) + 1
Next
Next
ReDim crr(1 To 2, 1 To UBound(brr, 2)) As Long
For Each key In dic(0).keys
n = n + 1: crr(1, n) = key
If dic(1).exists(key) Then crr(2, n) = dic(1)(key)
Next
For j = 1 To UBound(crr, 2) - 1
For k = j + 1 To UBound(crr, 2)
If crr(2, j) < crr(2, k) Then
flag = True
ElseIf crr(2, j) = crr(2, k) Then
If crr(1, j) > crr(1, k) Then flag = True
End If
If flag Then
t = crr(1, j): crr(1, j) = crr(1, k): crr(1, k) = t
t = crr(2, j): crr(2, j) = crr(2, k): crr(2, k) = t
flag = False
End If
Next
Next
m = m + 1
For j = 1 To UBound(brr, 2)
brr(m, j) = crr(1, j)
Next
dic(1).RemoveAll: n = 0
Next
[d5].Resize(m, UBound(brr, 2)) = brr
End Sub |