Try this
Option Explicit
Sub test()
Dim a, i As Long, ii As Long, iii As Long
Dim myColor As Long, n As Long, w, SL As Object
Set SL = CreateObject("System.Collections.SortedList")
With Range("myrange") '<---- change here to real name
a = .Value
For i = 2 To UBound(a, 1)
If Not SL.contains(a(i, 1)) Then
Set SL(a(i, 1)) = CreateObject("System.Collections.SortedList")
End If
myColor = .Cells(i, 1).Font.ColorIndex
If Not SL(a(i, 1)).contains(myColor) Then
ReDim w(1 To UBound(a, 2), 1 To 1)
Else
w = SL(a(i, 1))(myColor)
ReDim preservew(1 To UBound(a, 2), 1 To UBound(w, 2) + 1)
End If
For ii = 1 To UBound(a, 2)
w(ii, UBound(w, 2)) = a(i, ii)
Next
SL(a(i, 1))(myColor) = w
Next
With .Offset(1)
.ClearContents: .Font.ColorIndex = xlAutomatic: n = 1
For i = 0 To SL.Count - 1
For ii = 0 To SL.GetByIndex(i).Count - 1
With .Rows(n).Resize(UBound(SL.GetByIndex(i).GetByIndex(ii), 2))
.Value = Application.Index(SL.GetByIndex(i).GetByIndex(ii), 0, 0)
.Font.ColorIndex = SL.GetByIndex(i).GetKey(ii)
End With
n = n + UBound(SL.GetByIndex(i).GetByIndex(ii), 2)
Next
Next
End With
End With
End Sub
Bookmarks