Try this one.
Option Explicit
Sub test()
Dim a, i As Long, ii As Long, dic As Object
Dim e, s, v, t As Long, n As Long, txt As String
Set dic = CreateObject("Scripting.Dictionary")
dic.CompareMode = 1
Sheets("sheet2").Cells.ClearContents
a = Sheets("sheet1").Cells(1).CurrentRegion.Value
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For ii = 2 To UBound(a, 2) - 2
For i = 2 To UBound(a, 1)
If Not dic.exists(a(i, UBound(a, 2))) Then
n = n + 1
dic(a(i, UBound(a, 2))) = n
End If
If Not .exists(a(1, ii)) Then
Set .Item(a(1, ii)) = _
CreateObject("Scripting.Dictionary")
.Item(a(1, ii)).CompareMode = 1
End If
txt = Join$(Array(Year(a(i, 1)), a(i, UBound(a, 2) - 1)), Chr(2))
If Not .Item(a(1, ii)).exists(txt) Then
Set .Item(a(1, ii))(txt) = _
CreateObject("Scripting.Dictionary")
End If
.Item(a(1, ii))(txt)(a(i, UBound(a, 2))) = a(i, ii)
Next
Next
For Each e In .keys
ReDim a(1 To .Item(e).Count + 1, 1 To dic.Count + 1)
a(1, 1) = e: i = 1
For Each s In dic
i = i + 1
a(1, i) = s
Next
i = 1
For Each s In .Item(e).keys
i = i + 1
a(i, 1) = Split(s, Chr(2))(1)
For Each v In .Item(e)(s).keys
a(i, dic(v) + 1) = .Item(e)(s)(v)
Next
Next
Sheets("sheet2").Cells(1, t + 2) _
.Resize(UBound(a, 1), UBound(a, 2)).Value = a
t = t + UBound(a, 2) + 1
Next
End With
With Sheets("sheet2")
.UsedRange.Columns.AutoFit
.Activate
End With
End Sub
Bookmarks