|
Option Explicit
Sub TEST6()
Dim ar, br, cr(), i&, j&, k&, r&, dic As Object, vKey
Application.ScreenUpdating = False
Set dic = CreateObject("Scripting.Dictionary")
ar = Worksheets(2).[B2].CurrentRegion.Value
For i = 2 To UBound(ar)
dic(ar(i, 1)) = dic(ar(i, 1)) & " " & i
Next i
For Each vKey In dic.keys
br = Split(dic(vKey))
r = 0: Erase cr
For j = 1 To UBound(br)
r = r + 1
ReDim Preserve cr(1 To 2, 1 To r)
cr(1, r) = ar(br(j), 2)
cr(2, r) = ar(br(j), 3)
Next j
dic(vKey) = cr
Next
With Worksheets(1)
With [B2].CurrentRegion
.Offset(1, 3).ClearContents
ar = .Value
For i = 2 To UBound(ar)
If dic.exists(ar(i, 1)) Then
br = dic(ar(i, 1)): r = 64
bSort br, 1, 2, 1, UBound(br, 2), 2
r = 64
For k = 1 To UBound(br, 2)
For j = 5 To UBound(ar, 2)
If ar(1, j) >= br(1, k) Then
r = r + 1
ar(i, j) = Chr(r)
Exit For
End If
Next j
Next k
End If
Next i
.Value = ar
End With
End With
Set dic = Nothing
Application.ScreenUpdating = True
Beep
End Sub
Function bSort(ByRef ar, ByVal iFirst&, ByVal iLast&, ByVal iLeft&, _
ByVal iRight&, ByVal iKey&, Optional isOrder As Boolean = True)
Dim i&, j&, k&, vTemp
For i = iLeft To iRight - 1
For j = iLeft To iRight + iLeft - 1 - i
If ar(iKey, j) <> ar(iKey, j + 1) Then
If ar(iKey, j) < ar(iKey, j + 1) Xor isOrder Then
For k = iFirst To iLast
vTemp = ar(k, j)
ar(k, j) = ar(k, j + 1)
ar(k, j + 1) = vTemp
Next
End If
End If
Next j
Next i
End Function
|
|