Dynamic SUM of multiple named tables (structured referencing)

leolapa

New Member
Joined
Jun 3, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
The sample schedule below presents 3 sets of bank statements from different accounts/institutions, each one under separate named tables (Stmt1, Stmt2, Stmt3). By using structured referencing it helps the effort of keeping track of the latest balance for each statement as they keep growing over time.

At the very top, cell F1 dynamically sums the latest balance (last populated row) of each statement in order to inform the user the grand total of cash "on hand":
Excel Formula:
=SUM(LOOKUP(2;1/(Stmt1[Balance]<>"");Stmt1[Balance]);LOOKUP(2;1/(Stmt2[Balance]<>"");Stmt2[Balance]);LOOKUP(2;1/(Stmt3[Balance]<>"");Stmt3[Balance]))

So far, so good........ until there's always a BUT.........

In case I keep opening up new checking/savings/trading accounts and therefore new statements (Stmt4, Stmt5 and so on) start popping up to the right of the existing ones (column O onwards), HOW CAN I MAKE THE FORMULA ON F1 TO AUTOMATICALLY/DYNAMICALLY ADD THOSE NEW NAMED TABLES TO ITS SUM?

The obvious answer on such simple schedule is to just manually copy/paste the LOOKUP formula and change to the newly added table name. However my real life workbook is way more complex than that, and such task would not only be a chore but I also risk forgetting to incorporate those new statements to the tally formula. Thus I need a formula that recognizes as new statements are added so I don't have to worry about doing that in the future, and without recurring to VBA coding or helper columns.

1D Spilled Sum Last Non-Empty Rows.png


So I started my attempt to get to a solution by naming each table the same + a sequential number 1, 2, 3, etc. as this allows a recognition mechanism as new tables are added. Then I created a SEQUENCE formula based on the COUNTA of statement titles on row 3:
Excel Formula:
=SEQUENCE(COUNTA($3:$3))

And this enabled me to concatenate the structured referencing for all "Balance" columns on each statement:
Excel Formula:
="Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3;$3:$3))))&"["&Stmt1[[#Headers];[Balance]]&"]"

And as shown on the screenshot that follows I seemed to be on the right track to get to an INDIRECT "spilled" type solution that should do the trick just fine...

1D Spilled Sum Last Non-Empty Rows SEQUENCE, INDIRECT Text Spill.png


But that's when things started to go south......

I tried 4 different approaches to dynamically pick up the latest balance/last populated row of each statement table, but none of them worked in a way that would provide a dynamic solution that will ensure all future statements be integrated in the sum without further finagling.

My first go was by using the same LOOKUP(2;1/.... approach that returns the last non-empty row of a column. But this one is a no starter as it does not return a cell reference, so SUBTOTAL won't even take it (SUM/AGGREGATE do take nested functions that return values, but won't deal with spilled ranges so they are not feasible options).
Excel Formula:
[S]=SUBTOTAL(9;LOOKUP(2;1/(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(COUNTA($3:$3))))&"["&Stmt1[[#Headers];[Balance]]&"]")<>"");INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(COUNTA($3:$3))))&"["&Stmt1[[#Headers];[Balance]]&"]")))
[/S]


1D Spilled Sum Last Non-Empty Rows LOOKUP.png


Then I attempted 3 different combinations of INDEX, and they all managed to get to a "spilled" solution but returning ZERO amount, and by decomposing each formula I realized that despite achieving spilled solutions they all return errors.

MAX/ROW:
Excel Formula:
=SUBTOTAL(9;INDEX(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3;$3:$3))))&"["&Stmt1[[#Headers];[Balance]]&"]");MAX(ROW(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3;$3:$3))))&"["&Stmt1[[#Headers];[Balance]]&"]")))-ROW(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3;$3:$3))))&"[[#Headers];["&Stmt1[[#Headers];[Balance]]&"]]"))))

COUNTA:
Excel Formula:
=SUBTOTAL(9;INDEX(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3;$3:$3))))&"["&Stmt1[[#Headers];[Balance]]&"]");SUBTOTAL(3;INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3;$3:$3))))&"["&Stmt1[[#Headers];[Balance]]&"]"))))

INDIRECT/ADDRESS:
Excel Formula:
=SUBTOTAL(9;INDIRECT(ADDRESS(MAX(ROW(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3;$3:$3))))&"["&Stmt1[[#Headers];[Balance]]&"]")));COLUMN(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3;$3:$3))))&"["&Stmt1[[#Headers];[Balance]]&"]")))))

It can be seen on each screenshot for every approach that they all do work individually (ORANGE sections), but this way I would get to the same static formula that won't automatically incorporate further statements.

What am I doing wrong here? Is there any other way to go about that I can't seem to grasp? Any help on this is greatly appreciated!

Below I added screenshots for each of the above mentioned INDEX combinations...

INDEX + MAX/ROW:
1D Spilled Sum Last Non-Empty Rows INDEX, MAX, ROW.png


INDEX +COUNTA:
1D Spilled Sum Last Non-Empty Rows INDEX, COUNTA.png


INDEX + INDIRECT/ADDRESS:
1D Spilled Sum Last Non-Empty Rows INDEX, INDIRECT, ADDRESS.png


Minisheet:
1D Spilled Sum Last Non-Empty Row from Several Tables (Dynamic) MINISHEET.xlsx
ABCDEFGHIJKLMN
1Total balance, ALL checking accounts →$77.229,48← STATIC FORMULA WON'T ALLOW DYNAMIC INCLUSION OF NEW STATEMENTS TABLES TO THE RIGHT OF THE 3 EXISTING ONES!!!
2
3Bank of AmericaJPMorgan ChaseUS Bank
4
5DateTransactionAmountBalanceDateTransactionAmountBalanceDateTransactionAmountBalance
6Oct-20-2016Beginning balance$0,00$0,00Mar-12-2020Beginning balance$0,00$0,00May-2-2021Beginning balance$0,00$0,00
7Oct-21-2016Deposit$10.000,00$10.000,00Mar-12-2020Deposit$330.712,94$330.712,94May-3-2021Deposit$6,72$6,72
8Nov-1-2016Wire transfer out-$5.300,00$4.700,00Mar-16-2020Investment instrument-$30.000,00$300.712,94
9Nov-1-2016Folio 1-$1.550,64$3.149,36Mar-16-2020Investment instrument-$50.000,00$250.712,94
10Nov-1-2016Folio 2-$129,69$3.019,67Mar-17-2020Investment instrument-$50.000,00$200.712,94
11Nov-3-2016Folio 3-$1.774,75$1.244,92Mar-19-2020Investment instrument-$50.000,00$150.712,94
12Nov-4-2016Debit card-$174,96$1.069,96Mar-19-2020Investment instrument-$50.000,00$100.712,94
13Nov-8-2016Debit card-$60,17$1.009,79Apr-1-2020Investment instrument-$25.000,00$75.712,94
14Nov-9-2016Debit card-$20,80$988,99Apr-24-2020Wire transfer out-$25.000,00$50.712,94
15Nov-10-2016Debit card-$16,50$972,49Jun-23-2020Deposit$7.000,00$57.712,94
16Nov-10-2016Deposit$2.904,67$3.877,16Jun-27-2020Deposit$4.000,00$61.712,94
17Nov-11-2016Debit card-$36,30$3.840,86Jun-29-2020Deposit$4.000,00$65.712,94
18Nov-14-2016Debit card-$13,50$3.827,36SEQUENCE/ COUNT stmtsColumns text for INDIRECTLOOKUP and INDIRECTSUBTOTAL9HYPOTHETICAL FULL "SPILL" FORMULA THAT WON'T WORK
19Nov-16-2016Folio 4-$8.158,67$4.331,31(should spill, if source OK)
20Nov-16-2016Deposit$5.000,00$668,69(spills OK)(spills OK)(DOES NOT SPILL!)
21Nov-22-2016Folio 5-$275,47$393,22
22Nov-22-2016Deposit$2.904,67$3.297,891Stmt1[Balance]#N/A#N/A=SUBTOTAL(9;LOOKUP(2;1/(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3;$3:$3))))&"["&Stmt1[[#Headers];[Balance]]&"]")<>"");INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3;$3:$3))))&"["&Stmt1[[#Headers];[Balance]]&"]")))
23Nov-22-2016Debit card-$31,59$3.266,302Stmt2[Balance]APPROACH ONLY WORKS INDIVIDUALLYNO "#" array, so NO "spill" formula
24Nov-28-2016Debit card-$25,60$3.240,703Stmt3[Balance]
25Nov-29-2016Cash withdrawal-$200,00$3.040,70Stmt1[[#Headers];[Balance]]
26Nov-30-2016TED Recebida$8.600,00$11.640,70Stmt2[[#Headers];[Balance]]
27Nov-30-2016Debit card-$14,90$11.625,80Stmt3[[#Headers];[Balance]]$11.509,82$0,00
28Dec-1-2016Debit card-$20,50$11.605,30$65.712,94
29Dec-1-2016Folio 6-$95,48$11.509,82$6,72
30
31INDEX/MAX/ROW and INDIRECTSUBTOTAL9FULL "SPILL" FORMULA NO LONGER RETURNS AN ERROR, BUT STILL WON'T WORK
32(should spill, if source OK)
33(Spills, but with ERROR!)
34
35#VALUE!#VALUE!$0,00
36#VALUE!$0,00
37#VALUE!$0,00
38APPROACH ONLY WORKS INDIVIDUALLYNO "#" array, so NO "spill" formula
39
40
41
42$11.509,82$77.229,48
43$65.712,94
44$6,72
45
46INDEX/COUNTA (SUBTOTAL3) and INDIRECTSUBTOTAL9FULL "SPILL" FORMULA NO LONGER RETURNS AN ERROR, BUT STILL WON'T WORK
47(should spill, if source OK)
48(Spills, but with ERROR!)
49
50#VALUE!$0,00$0,00
51#VALUE!$0,00
52#VALUE!$0,00
53APPROACH ONLY WORKS INDIVIDUALLY (using COUNTA)NO "#" array, so NO "spill" formula
54
55
56
57$11.509,82$77.229,48
58$65.712,94
59$6,72
60
61INDIRECT and ADDRESSSUBTOTAL9FULL "SPILL" FORMULA NO LONGER RETURNS AN ERROR, BUT STILL WON'T WORK
62(should spill, if source OK)
63(Spills, but with ERROR!)
64
65#VALUE!#VALUE!$0,00
66#VALUE!$0,00
67#VALUE!$0,00
68APPROACH ONLY WORKS INDIVIDUALLY (using COUNTA)NO "#" array, so NO "spill" formula
69
70
71
72$11.509,82$77.229,48
73$65.712,94
74$6,72
1D Sum Last Rows
Cell Formulas
RangeFormula
F1F1=SUM(LOOKUP(2,1/(Stmt1[Balance]<>""),Stmt1[Balance]),LOOKUP(2,1/(Stmt2[Balance]<>""),Stmt2[Balance]),LOOKUP(2,1/(Stmt3[Balance]<>""),Stmt3[Balance]))
G6,L6G6=Stmt1[@Transaction]
N6:N7,D6:D29,I6:I17N6=AGGREGATE(9,6,M$6:M6)
F22:F24F22=SEQUENCE(SUBTOTAL(3,$3:$3))
G22:G24G22="Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3,$3:$3))))&"["&Stmt1[[#Headers],[Balance]]&"]"
H22H22=LOOKUP(2,1/(INDIRECT($G$22#)<>""),INDIRECT($G$22#))
K22K22=SUBTOTAL(9,$H$22)
G25:G27G25="Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3,$3:$3))))&"[[#Headers];["&Stmt1[[#Headers],[Balance]]&"]]"
K27K27=SUBTOTAL(9,$I$27:$J$29)
H27:H29H27=LOOKUP(2,1/(INDIRECT($G22)<>""),INDIRECT($G22))
I35:I37I35=INDEX(INDIRECT($G$22#),MAX(ROW(INDIRECT($G$22#)))-ROW(INDIRECT($G$25#)))
K35,K65,K50K35=SUBTOTAL(9,I35#)
M35:M37M35=SUBTOTAL(9,INDEX(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3,$3:$3))))&"["&Stmt1[[#Headers],[Balance]]&"]"),MAX(ROW(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3,$3:$3))))&"["&Stmt1[[#Headers],[Balance]]&"]")))-ROW(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3,$3:$3))))&"[[#Headers];["&Stmt1[[#Headers],[Balance]]&"]]"))))
K42K42=SUBTOTAL(9,$H$42:$I$44)
H42:H44H42=INDEX(INDIRECT($G22),MAX(ROW(INDIRECT($G22)))-ROW(INDIRECT($G25)))
I50:I52I50=INDEX(INDIRECT($G$22#),SUBTOTAL(3,INDIRECT($G$22#)))
M50:M52M50=SUBTOTAL(9,INDEX(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3,$3:$3))))&"["&Stmt1[[#Headers],[Balance]]&"]"),SUBTOTAL(3,INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3,$3:$3))))&"["&Stmt1[[#Headers],[Balance]]&"]"))))
K57K57=SUBTOTAL(9,$H$57:$I$59)
H57:H59H57=INDEX(INDIRECT($G22),COUNTA(INDIRECT($G22)))
I65:I67I65=INDIRECT(ADDRESS(MAX(ROW(INDIRECT($G$22#))),COLUMN(INDIRECT($G$22#))))
M65:M67M65=SUBTOTAL(9,INDIRECT(ADDRESS(MAX(ROW(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3,$3:$3))))&"["&Stmt1[[#Headers],[Balance]]&"]"))),COLUMN(INDIRECT("Stmt"&SEQUENCE(COUNTA(SEQUENCE(SUBTOTAL(3,$3:$3))))&"["&Stmt1[[#Headers],[Balance]]&"]")))))
K72K72=SUBTOTAL(9,$H$72:$I$74)
H72:H74H72=INDIRECT(ADDRESS(MAX(ROW(INDIRECT($G22))),COLUMN(INDIRECT($G22))))
Dynamic array formulas.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Dynamic SUM of multiple named tables (structured referencing)
and Dynamic SUM of multiple named tables (structured referencing)
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
I appreciate your heads up on this and apologize for the inconvenience. I'll make sure to post the links next time...
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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