Populate headers based on column and merge amount for multiple words

Omar M

Board Regular
Joined
Jan 11, 2024
Messages
66
Office Version
  1. 2019
Platform
  1. Windows
Hello
I want to make macro in columns I:N based on cells I2,K2
so if I2,K2 are empty then should populate headers from I:M and insert column OUT in column M to sum column J,L,M based on items are existed in column D.
in column I will brings items from column E as to rest of columns will merge amounts in column F and put under headers for each item in column I
I want macro eal with 11000 rows from columns A:F.
example :

CVF ‫‬.xlsx
ABCDEFGHIJK
1ITEM DATEINVOICE NO CONDITIONINVOICE TYPTOTALFROM DATETO DATE
2101/11/2023SSLL-001PAIDSL60,147.00
3204/11/2023SSLL-002NOT PAIDSL9,504.00
4305/11/2023SSLL-003NOT PAIDSL7,315.00
5406/11/2023SSLL-004NOT PAIDSL53,234.00
6509/11/2023SSLL-005NOT PAIDSL3,706.00
7610/11/2023SSLL-006PAIDSL2,904.00
8712/11/2023SSLL-007CASH BY SAFESL15,000.00
9813/11/2023SSLL-008CASH BY BANKSL32,452.00
10915/01/2023MMLLL-001PAIDML1,342.00
111015/01/2023MMLLL-002PAIDML2,082.00
121116/01/2023MMLLL-003NOT PAIDML3,798.00
131206/11/2023MMLLL-004CASH BY BANKML3,200.00
141307/11/2023MMLLL-005CASH BY SAFEML1,020.00
151417/11/2023TTLL-001CASH BY BANKTL12,540.00
161517/11/2023TTLL-002NOT PAIDTL2,900.00
171618/11/2023TTLL-003NOT PAIDTL1,500.00
181718/11/2023TTLL-004PAIDTL2,816.00
191818/11/2023TTLL-005PAIDTL15,000.00
201918/11/2023TTLL-006CASH BY SAFETL9,450.00
OUT



result
CVF ‫‬.xlsx
ABCDEFGHIJKLMN
1ITEM DATEINVOICE NO CONDITIONINVOICE TYPTOTALFROM DATETO DATE
2101/11/2023SSLL-001PAIDSL60,147.00
3204/11/2023SSLL-002NOT PAIDSL9,504.00
4305/11/2023SSLL-003NOT PAIDSL7,315.00INVOICE TYPPAIDNOT PAIDCASH BY SAFECASH BY BANKOUT
5406/11/2023SSLL-004NOT PAIDSL53,234.00SL63,501.0073,759.0015,000.0032,452.00110,953.00
6509/11/2023SSLL-005NOT PAIDSL3,706.00ML3,424.003,798.001,020.003,200.007,644.00
7610/11/2023SSLL-006PAIDSL2,904.00TL17,816.004,400.009,450.0012,540.0039,806.00
8712/11/2023SSLL-007CASH BY SAFESL15,000.00TOTAL42,261.0081,957.004,530.0016,712.0063,503.00
9813/11/2023SSLL-008CASH BY BANKSL32,452.00
10915/01/2023MMLLL-001PAIDML1,342.00
111015/01/2023MMLLL-002PAIDML2,082.00
121116/01/2023MMLLL-003NOT PAIDML3,798.00
131206/11/2023MMLLL-004CASH BY BANKML3,200.00
141307/11/2023MMLLL-005CASH BY SAFEML1,020.00
151417/11/2023TTLL-001CASH BY BANKTL12,540.00
161517/11/2023TTLL-002NOT PAIDTL2,900.00
171618/11/2023TTLL-003NOT PAIDTL1,500.00
181718/11/2023TTLL-004PAIDTL2,816.00
191818/11/2023TTLL-005PAIDTL15,000.00
201918/11/2023TTLL-006CASH BY SAFETL9,450.00
OUT
Cell Formulas
RangeFormula
N5:N8N5=J5+L5+M5
J8,L8:M8J8=J5-J6-J7
K8K8=SUM(K5:K7)


and when use dates then should be
CVF ‫‬.xlsx
ABCDEFGHIJKLMN
1ITEM DATEINVOICE NO CONDITIONINVOICE TYPTOTALFROM DATETO DATE
2101/11/2023SSLL-001PAIDSL60,147.0001/11/202317/11/2023
3204/11/2023SSLL-002NOT PAIDSL9,504.00
4305/11/2023SSLL-003NOT PAIDSL7,315.00INVOICE TYPPAIDNOT PAIDCASH BY SAFECASH BY BANKOUT
5406/11/2023SSLL-004NOT PAIDSL53,234.00SL63,501.0073,759.0015,000.0032,452.00110,953.00
6509/11/2023SSLL-005NOT PAIDSL3,706.00ML3,424.003,798.001,020.003,200.007,644.00
7610/11/2023SSLL-006PAIDSL2,904.00TL-2,900.00-12,540.0012,540.00
8712/11/2023SSLL-007CASH BY SAFESL15,000.00TOTAL60,077.0080,457.0013,980.0016,712.0090,769.00
9813/11/2023SSLL-008CASH BY BANKSL32,452.00
10915/01/2023MMLLL-001PAIDML1,342.00
111015/01/2023MMLLL-002PAIDML2,082.00
121116/01/2023MMLLL-003NOT PAIDML3,798.00
131206/11/2023MMLLL-004CASH BY BANKML3,200.00
141307/11/2023MMLLL-005CASH BY SAFEML1,020.00
151417/11/2023TTLL-001CASH BY BANKTL12,540.00
161517/11/2023TTLL-002NOT PAIDTL2,900.00
171618/11/2023TTLL-003NOT PAIDTL1,500.00
181718/11/2023TTLL-004PAIDTL2,816.00
191818/11/2023TTLL-005PAIDTL15,000.00
201918/11/2023TTLL-006CASH BY SAFETL9,450.00
OUT
Cell Formulas
RangeFormula
N5:N8N5=J5+L5+M5
J8,L8:M8J8=J5-J6-J7
K8K8=SUM(K5:K7)



NOTE: I would solution by vba . I don't want power query at all.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I have some doubts:

1. They are always the same INVOICE TYP, they always go in this order: SL, ML, TL

2. If an INVOICE TYP has no values, do you want it to appear with 0 in all columns?

3. The formulas in row 8 (TOTAL) are always the same, so they won't increase the number of INVOICE TYP?

4. They are always the same CONDITION, they always go in this order: PAID, NOT PAID, CASH BY SAFE, CASH BY BANK, OUT

5. If an CONDITION has no values, do you want it to appear with 0 in all rows?

----- --
I hope to hear from you soon.
Respectfully
Dante Amor
----- --
 
Upvote 0
1. They are always the same INVOICE TYP, they always go in this order: SL, ML, TL
well, there are others INVOICE TYPE , it's not necessary to be the same orders.
2. If an INVOICE TYP has no values, do you want it to appear with 0 in all columns?
I'm not sure what you mean in all columns!
any INVOICE TYP has no values for specific column then should show zero as hyphen.
3. The formulas in row 8 (TOTAL) are always the same, so they won't increase the number of INVOICE TYP?
yes even there are others numbers of INVOICE TYP( it just will be for SL,ML,TL .
4. They are always the same CONDITION, they always go in this order: PAID, NOT PAID, CASH BY SAFE, CASH BY BANK, OUT
will be others conditions and , orders will be who shows first based on original data .
5. If an CONDITION has no values, do you want it to appear with 0 in all rows?
will be zero under header doesn't existed in original data.
 
Upvote 0
Using Power Query, I created a Parameter Query

Book2
ABCDEFGHIJKLM
1ITEM DATEINVOICE NO CONDITIONINVOICE TYPTOTALFROM DATETO DATE
2111/1/2023SSLL-001PAIDSL6014711/1/202311/17/2023
3211/4/2023SSLL-002NOT PAIDSL9504
4311/5/2023SSLL-003NOT PAIDSL7315
5411/6/2023SSLL-004NOT PAIDSL53234INVOICE TYPPAIDNOT PAIDCASH BY SAFECASH BY BANK
6511/9/2023SSLL-005NOT PAIDSL3706ML10203200
7611/10/2023SSLL-006PAIDSL2904SL63051737591500032452
8711/12/2023SSLL-007CASH BY SAFESL15000TL290012540
9811/13/2023SSLL-008CASH BY BANKSL32452
1091/15/2023MMLLL-001PAIDML1342
11101/15/2023MMLLL-002PAIDML2082
12111/16/2023MMLLL-003NOT PAIDML3798
131211/6/2023MMLLL-004CASH BY BANKML3200
141311/7/2023MMLLL-005CASH BY SAFEML1020
151411/17/2023TTLL-001CASH BY BANKTL12540
161511/17/2023TTLL-002NOT PAIDTL2900
171611/18/2023TTLL-003NOT PAIDTL1500
181711/18/2023TTLL-004PAIDTL2816
191811/18/2023TTLL-005PAIDTL15000
201911/18/2023TTLL-006CASH BY SAFETL9450
Sheet1

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}, {"TOTAL", Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each [DATE] >= Table2 and [DATE] <= Table3),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"CONDITION", "INVOICE TYP"}, {{"totals", each List.Sum([TOTAL]), type nullable number}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[CONDITION]), "CONDITION", "totals")
in
    #"Pivoted Column"
Table2 and Table3 are your look up dates
 
Upvote 0
@alansidman
first thank you
second you don't have TOTAL row and OUT column
finally I said
NOTE: I would solution by vba . I don't want power query at all.
I don't like Power Query , I don't want it at all !
 
Upvote 0
well, there are others INVOICE TYPE , it's not necessary to be the same orders.

This formula causes me a lot of noise:
Excel Formula:
=J5-J6-J7

And no matter the order, this is not the same:
Excel Formula:
63501 - 3424 - 17816 = 42261

Than this:
Excel Formula:
3424 - 63501 - 17816 = -77893

I hope to explain what my concern is. If there are more INVOICE TYPs, you must explain how they should look and what the formula should be like.
I have no problems with formulas that add (the order of the addends does not alter the sum).

--------------------------------------------------
I still can't help you, since you didn't adequately answer my questions.

1. I need you to clarify how the remaining formulas will look if there are more INVOICE TYPs. (=J5-J6-J7 , =L5-L6-L7, =M5-M6-M7)

2. Are you going to put the headings in columns I to N? or is the macro going to put them?
 
Upvote 0
Try the following macro. Put the invoices and the columns.

I warn you: it does not put totals nor does it put the values in the OUT column, that is because you have not defined what the formula is like in case the number of columns increases or the number of rows increases.

Also, what happens if the columns and rows appear arranged in a different order, since they are currently placed as they appear.

VBA Code:
Sub Invoice_Typ()
  Dim a As Variant, b As Variant
  Dim dicR As Object, dicC As Object
  Dim i As Long, j As Long, nRow As Long, nCol As Long
  Dim froDate As Long, to_Date As Long
  Dim iniDate As Long, finDate As Long
    
  Set dicR = CreateObject("Scripting.Dictionary")   'Invoice
  Set dicC = CreateObject("Scripting.Dictionary")   'Condition
  Range("I4", Cells(Rows.Count, Columns.Count)).Clear
  
  a = Range("A1", Range("F" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a) + 2, 1 To UBound(a) + 2)
  froDate = Range("I2").Value
  to_Date = Range("K2").Value
  
  dicC("INVOICE TYP") = Empty
  For i = 2 To UBound(a)
  
    If froDate = 0 Then iniDate = a(i, 2) Else iniDate = froDate
    If to_Date = 0 Then finDate = a(i, 2) Else finDate = to_Date
  
    If a(i, 2) >= iniDate And a(i, 2) <= finDate Then
      If Not dicR.exists(a(i, 5)) Then dicR(a(i, 5)) = dicR.Count + 1 'Invoice
      If Not dicC.exists(a(i, 4)) Then dicC(a(i, 4)) = dicC.Count     'Condition
  
      nRow = dicR(a(i, 5))
      nCol = dicC(a(i, 4))
      b(nRow, nCol) = b(nRow, nCol) + a(i, 6)
    End If
  Next
  
  For i = 1 To dicR.Count + 1
    For j = 1 To dicC.Count
      If b(i, j) = "" Then b(i, j) = 0
    Next
  Next
  
  'Headers
  dicC("OUT") = Empty
  dicR("TOTAL") = Empty
  With Range("I4").Resize(1, dicC.Count)
    .Value = dicC.keys
    .Borders.Color = Range("A1").Borders.Color
    .Interior.Color = Range("A1").Interior.Color
    .HorizontalAlignment = xlCenter
    .Font.Bold = True
  End With
  
  'Invoice Typ
  Range("I5").Resize(dicR.Count).Value = Application.Transpose(dicR.keys)
  
  'Values
  Range("J5").Resize(dicR.Count, dicC.Count).Value = b
  With Range("I5").Resize(dicR.Count, dicC.Count)
    .Select
    .Borders.Color = Range("A1").Borders.Color
    .HorizontalAlignment = xlCenter
    .NumberFormat = "#,##0.00;-#,##0.00;-"
    With .Offset(dicR.Count - 1).Cells(1)
      .Font.Bold = True
      .Interior.Color = Range("A1").Interior.Color
    End With
  End With
  
  'Totals
  
End Sub
 
Upvote 0
Solution
now I understood my big mistake it will not be the same formula when add new INVOICE TYP ,CONDITION whether in OUT column or TOTAL row, sorry for this confusion. I will create new thread to treat this problem ,I will think idea how should show data.
your code works perfectly .
thank you so much.:)
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top