Sub test()
Dim a, s, i As Long, ii As Long, col, hd, dic As Object
Set dic = CreateObject("Scripting.Dictionary")
a = Sheets("data").[a1].CurrentRegion.Resize(, 9).Value
col = Array(3, 2, 8, 9, 6, 7)
For i = 2 To UBound(a, 1)
If Not dic.exists(a(i, 2)) Then Set dic(a(i, 2)) = CreateObject("Scripting.Dictionary")
s = Format$(a(i, 1), "yyyy mm")
If Not dic(a(i, 2)).exists(s) Then Set dic(a(i, 2))(s) = CreateObject("Scripting.Dictionary")
If Not dic(a(i, 2))(s).exists(a(i, 3)) Then
ReDim w(1 To UBound(col) + 1)
For ii = 0 To UBound(col)
w(ii + 1) = a(i, col(ii))
Next
Else
w = dic(a(i, 2))(s)(a(i, 3))
For ii = 2 To UBound(col)
w(ii + 1) = w(ii + 1) + a(i, col(ii))
Next
End If
dic(a(i, 2))(s)(a(i, 3)) = w
Next
hd = Array("Product ID", "Product Name", "Sale", "Comments", "Stock", "Pay")
OutPut dic, col, hd
End Sub
Sub OutPut(dic As Object, col, hd)
Dim e, s, n As Long
Application.ScreenUpdating = False
For Each e In dic
If Not Evaluate("isref('" & e & "'!a1)") Then Sheets.Add(, Sheets(Sheets.Count)).Name = e
With Sheets(e)
.Cells.Clear: .Cells.HorizontalAlignment = xlCenter
.Cells.Font.Name = "Verdana": .Cells.Font.Size = 11
With .[a1:f1]
.Merge: .Font.Bold = True: n = 3
.Cells(1) = "Monthly Wise Report: " & e & " (All Types)"
.BorderAround Weight:=2
End With
For Each s In dic(e)
With .Cells(n, "b").Resize(2, 5)
.Font.Bold = True: .Borders.Weight = 2: n = n + 3
.Rows(1) = Array("Month", "Manager", "", "Admin", "Accounts")
With .Cells(1, 2).Resize(2, 2)
.HorizontalAlignment = 7
.Range("a1:a2").Borders(10).LineStyle = xlNone
End With
.Cells(2, 1) = "'" & Format$(DateSerial(Split(s)(0), Split(s)(1), 1), "mmm-yyyy")
End With
With .Cells(n, "b").Resize(, 5)
.Merge: .Font.Bold = True: n = n + 1
.Cells(1) = ":::Monthly Detail:::"
End With
With .Cells(n, "a").Resize(dic(e)(s).Count + 1, 6)
.Rows(1) = hd: .Rows(1).Font.Bold = True
With .Rows(2).Resize(dic(e)(s).Count)
.Value = Application.Index(dic(e)(s).items, 0, 0)
.Sort .Cells(1)
End With
.Borders.Weight = 2
With .Cells(.Rows.Count + 1, 6)
.Font.Bold = True
.FormulaR1C1 = "=sum(r" & n + 1 & "c:r[-1]c)"
.Borders.Weight = 2
End With
n = n + .Rows.Count + 2
End With
Next
.Columns("d").Replace 0, "", 1
.Columns("a:f").ColumnWidth = 17.89
.Rows.RowHeight = 18
End With
Next
Application.ScreenUpdating = True
End Sub
Bookmarks