Sub test()
Dim a, b, i As Long, n As Long, maxRow As Long
With Sheets("item category")
a = .Range("a7", .Range("a" & Rows.Count).End(xlUp)).Resize(, 5).Value
End With
ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 100)
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For i = 1 To UBound(a, 1)
If Not .exists(a(i, 5)) Then
.Item(a(i, 5)) = Array(1, .Count + 1)
If UBound(b, 2) < .Count Then
ReDim Preserve b(1 To UBound(b, 1), 1 To UBound(b, 2) + 100)
End If
b(1, .Count) = a(i, 5)
End If
n = .Item(a(i, 5))(0) + 1
b(n, .Item(a(i, 5))(1)) = a(i, 2)
.Item(a(i, 5)) = Array(n, .Item(a(i, 5))(1))
If maxRow < n Then maxRow = n
Next
ReDim Preserve b(1 To UBound(b, 1), 1 To .Count)
End With
With Sheets.Add.Cells(1).Resize(maxRow, UBound(b, 2))
.Value = b
.Columns.AutoFit
End With
End Sub
Bookmarks