Sub test()
Dim a, i As Long, ii As Long, iii As Long
Dim txt As String, cols As Long, SL As Object
Set SL = CreateObject("System.Collections.SortedList")
a = Sheets("data").Cells(1).CurrentRegion.Value
With CreateObject("System.Collections.SortedList")
For i = 2 To UBound(a, 1)
If Not SL.Contains(a(i, 4)) Then Set SL(a(i, 4)) = CreateObject("System.Collections.ArrayList")
If Not SL(a(i, 4)).Contains(a(i, 3)) Then SL(a(i, 4)).Add a(i, 3): cols = cols + 1
If Not .Contains(a(i, 2)) Then Set .Item(a(i, 2)) = CreateObject("Scripting.Dictionary")
txt = Join(Array(a(i, 4), a(i, 3)), Chr(2))
.Item(a(i, 2))(txt) = .Item(a(i, 2))(txt) + 1
Next
ReDim a(1 To .Count + 3, 1 To cols + 2)
a(1, 1) = "Cod Type": a(1, UBound(a, 2)) = "Grand Total"
a(UBound(a, 1), 1) = "Grand Total": iii = 1
For i = 0 To SL.Count - 1
SL.GetByIndex(i).Sort
For ii = 0 To SL.GetByIndex(i).Count - 1
iii = iii + 1
a(1, iii) = SL.GetKey(i)
a(2, iii) = SL.GetByIndex(i)(ii)
Next
Next
For i = 0 To .Count - 1
a(i + 3, 1) = .GetKey(i)
For ii = 2 To UBound(a, 2) - 1
a(i + 3, ii) = 0
txt = Join(Array(a(1, ii), a(2, ii)), Chr(2))
If .GetByIndex(i).exists(txt) Then a(i + 3, ii) = .GetByIndex(i)(txt)
a(i + 3, UBound(a, 2)) = a(i + 3, UBound(a, 2)) + a(i + 3, ii)
a(UBound(a, 1), UBound(a, 2)) = a(UBound(a, 1), UBound(a, 2)) + a(i + 3, ii)
a(UBound(a, 1), ii) = a(UBound(a, 1), ii) + a(i + 3, ii)
Next
Next
End With
GoOutPut a, SL
End Sub
Private Sub GoOutPut(a, SL As Object)
Dim i As Long, ii As Long, t As Long, myColor
Application.ScreenUpdating = False
Application.DisplayAlerts = False
myColor = Array(12961279, 16309473, 14348258)
With Sheets.Add.Range("b2").Resize(UBound(a, 1), UBound(a, 2))
.Value = a: .Borders.LineStyle = xlDot: .BorderAround Weight:=3
Union(.Range("a1:a2"), .Cells(.Rows.Count, 1), .Cells(1, .Columns.Count), _
.Cells(.Rows.Count, .Columns.Count)).BorderAround Weight:=3
.Offset(, 1).HorizontalAlignment = xlCenter
Union(.Columns(1), .Rows("1:2"), .Rows(.Rows.Count)).Font.Bold = True
.Cells(1, .Columns.Count).Resize(2).Merge: ii = 2
Union(.Rows("1:2"), .Rows(.Rows.Count)).Interior.Color = 10921638
With .Rows("1:2")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Font.Bold = True
End With
For i = 0 To SL.Count - 1
With .Cells(1, ii).Resize(, SL.GetByIndex(i).Count)
.Merge
.Resize(2).BorderAround Weight:=3
End With
With .Cells(3, ii).Resize(.Rows.Count - 3, SL.GetByIndex(i).Count)
.Interior.Color = myColor(t): t = t + 1
If t > UBound(myColor) Then t = 0
.BorderAround Weight:=3
End With
ii = ii + SL.GetByIndex(i).Count
Next
.Range("a1:a2").Merge
Union(.Columns(1), .Columns(.Columns.Count)).Columns.AutoFit
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Bookmarks