Sub test()
Dim a, e, s, v, i As Long, ii As Long, n As Long, dic(1) As Object
Dim invDate As Date, DueDate As Date, mngDate As String, adjDate As String
invDate = Sheets("input").[d4]: DueDate = invDate + 9
mngDate = Sheets("input").[g1].Text: adjDate = Sheets("input").[g2].Text
Set dic(0) = CreateObject("Scripting.Dictionary")
Set dic(1) = CreateObject("Scripting.Dictionary")
a = Sheets("description").[a2].CurrentRegion.Value
For i = 1 To UBound(a, 1)
dic(0)(a(i, 1)) = Array(a(i, 3), a(i, 4))
Next
a = Sheets("source").Cells(1).CurrentRegion.Value
For i = 2 To UBound(a, 1)
If Not dic(1).exists(a(i, 2)) Then
dic(1)(a(i, 2)) = Array(CreateObject("Scripting.Dictionary"), a(i, 1))
End If
If Not dic(1)(a(i, 2))(0).exists(a(i, 1)) Then
Set dic(1)(a(i, 2))(0)(a(i, 1)) = CreateObject("Scripting.Dictionary")
End If
For ii = 3 To UBound(a, 2)
If a(i, ii) <> "" Then dic(1)(a(i, 2))(0)(a(i, 1))(a(1, ii)) = a(i, ii)
Next
Next
ReDim a(1 To UBound(a, 1) * UBound(a, 2), 1 To 11)
For Each e In dic(1)
n = n + 1: a(n, 3) = invDate: a(n, 4) = DueDate: a(n, 5) = "Net 10"
a(n, 6) = "The total amount due for this invoice will be drafted via ACH on the due date shown."
a(n, 2) = e & IIf(dic(1)(e)(0).Count > 1, "", "_" & dic(1)(e)(1))
For Each s In dic(1)(e)(0)
For Each v In dic(1)(e)(0)(s)
a(n, 7) = dic(0)(v)(0) & IIf(dic(1)(e)(0).Count > 1, "_" & dic(1)(e)(1), "")
a(n, 8) = "Management Fee " & IIf(a(n, 7) Like "*?ADJ*", "Adjustment ", "") & _
"for Week Ending " & IIf(a(n, 7) Like "*?ADJ*", adjDate, mngDate) & ": " & _
dic(0)(v)(1)
a(n, 11) = dic(1)(e)(0)(s)(v)
n = n + 1
Next
Next
n = n - 1
Next
With Sheets("outcome").Cells(1).Resize(, 11)
.EntireColumn.ClearContents
.Value = Array("*InvoiceNo", "*Customer", "*InvoiceDate", "*DueDate", _
"Terms", "Memo", "Item (Product / Service)", "ItemDescription", _
"ItemQuantity", "ItemRate", "*ItemAmount")
.Rows(2).Resize(n) = a
.Columns(11).Resize(n + 1).NumberFormatLocal = _
"_(""$""* #,##0.00_);_(""$""* (#,##0.00);_(""$""* ""-""??_);_(@_)"
End With
End Sub
Bookmarks