SUM - N Rows above (Value of N comes from two columns one row above)

sm789

New Member
Joined
Aug 17, 2014
Messages
29
Office Version
  1. 2011
Platform
  1. MacOS
Dear All,

I am stuck and need a little help. Seems like a simple problem.

I have a list. Every day we get sales data from different locations. Each location gets one row. Then data goes in the subsequent columns. The number of rows is dynamic depending on how many locations reported for that day.

Here is the structure:

Column A has date, Column B has serial numbers of data points for that date, Column C has location, Column D through K has data for that location.
For each date, the row after the last location has totals. Here is the issue. Since the number of rows to total come from Column B (Column B is reset at 1 for each date), how do I create a SUM formula to add n number of rows when n is a value in one row above and two columns to the left?

Thank you
Sam
Screen Shot 2020-10-19 at 12.44.46 PM.png
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Would something like this work for you?

Put the formula in E7, copy across, then copy that row of formulas to the other TOTAL rows.

20 10 19.xlsm
ABCDEFG
1
212/10/2020416
312/10/2020382
412/10/2020692
512/10/2020838
612/10/2020872
712/10/2020TOTAL292820
813/10/2020773
913/10/2020625
1013/10/2020TOTAL1398
1114/10/2020696
1214/10/2020161
1314/10/2020592
1414/10/2020227
1514/10/2020934
1614/10/2020295
1714/10/2020283
1814/10/2020TOTAL274628
1915/10/2020926
2015/10/2020125
2115/10/2020856
2215/10/2020888
2315/10/2020413
2415/10/2020653
2515/10/2020547
2615/10/2020326
2715/10/2020453
2815/10/2020292
2915/10/2020418
3015/10/2020TOTAL544457
Sum Variable rows
Cell Formulas
RangeFormula
E7:G7,E30:G30,E18:G18,E10:G10E7=SUMIF($A$2:$A6,$A7,E$2:E6)
 
Upvote 0
Solution
Would something like this work for you?

Put the formula in E7, copy across, then copy that row of formulas to the other TOTAL rows.

20 10 19.xlsm
ABCDEFG
1
212/10/2020416
312/10/2020382
412/10/2020692
512/10/2020838
612/10/2020872
712/10/2020TOTAL292820
813/10/2020773
913/10/2020625
1013/10/2020TOTAL1398
1114/10/2020696
1214/10/2020161
1314/10/2020592
1414/10/2020227
1514/10/2020934
1614/10/2020295
1714/10/2020283
1814/10/2020TOTAL274628
1915/10/2020926
2015/10/2020125
2115/10/2020856
2215/10/2020888
2315/10/2020413
2415/10/2020653
2515/10/2020547
2615/10/2020326
2715/10/2020453
2815/10/2020292
2915/10/2020418
3015/10/2020TOTAL544457
Sum Variable rows
Cell Formulas
RangeFormula
E7:G7,E30:G30,E18:G18,E10:G10E7=SUMIF($A$2:$A6,$A7,E$2:E6)
Thank you Sir! This requires us to modify the Total formula each time depending on the number of rows above. I was hoping for a formula that can be copied without having to modify it. Basically I was wondering if it is possible to do something like SUM(from cell above to n number of rows; where n is from Column B of the row above). Thanks
 
Upvote 0
Sorry! I spoke too soon. This works for me. Simple and beautiful!

Thank you
 
Upvote 0
You're welcome.

You do have to adjust the first formula only each day to account for the number of rows in that first section, that is all.
The other option is to have a small macro to insert the formulas for you.

Example.
Before the macro

20 10 19.xlsm
ABCDEFGHIJK
112/10/202014162922
212/10/202023828339
312/10/202036924229
412/10/202048388327
512/10/202058726829
612/10/2020TOTAL
713/10/202017732929
813/10/202026252281
913/10/2020TOTAL
1014/10/202016967694
1114/10/202021616999
1214/10/202035926971
1314/10/202042277745
1414/10/202059342581
1514/10/202062953696
1614/10/202072833467
1714/10/2020TOTAL
1815/10/202019263411
1915/10/202021255756
2015/10/202038562672
2115/10/202048889343
2215/10/202054131195
2315/10/202066533489
2415/10/202075476182
2515/10/202083266315
2615/10/202094534342
2715/10/2020102925328
2815/10/2020114183844
2915/10/2020TOTAL
Sum Variable rows



Macro:
VBA Code:
Sub InsertTotalFormulas()
  Intersect(Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks).EntireRow, Columns("E:K")).FormulaR1C1 = "=SUMIF(R1C1:R[-1]C1,RC1,R1C:R[-1]C)"
End Sub


After the macro:

20 10 19.xlsm
ABCDEFGHIJK
112/10/202014162922
212/10/202023828339
312/10/202036924229
412/10/202048388327
512/10/202058726829
612/10/2020TOTAL29282028251136
713/10/202017732929
813/10/202026252281
913/10/2020TOTAL13984111010
1014/10/202016967694
1114/10/202021616999
1214/10/202035926971
1314/10/202042277745
1414/10/202059342581
1514/10/202062953696
1614/10/202072833467
1714/10/2020TOTAL27462834465233
1815/10/202019263411
1915/10/202021255756
2015/10/202038562672
2115/10/202048889343
2215/10/202054131195
2315/10/202066533489
2415/10/202075476182
2515/10/202083266315
2615/10/202094534342
2715/10/2020102925328
2815/10/2020114183844
2915/10/2020TOTAL54445747435347
Sum Variable rows
Cell Formulas
RangeFormula
E6:K6,E29:K29,E17:K17,E9:K9E6=SUMIF($A$1:$A5,$A6,E$1:E5)
 
Upvote 0
BTW, I suggest that you ..
  • update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
  • investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.
 
Upvote 0
Thank you very much! The formula works very well. I will try the vba macro as well simply because I love to play around with macros and learn new things.

Have updated my profile! Thank you again!
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,485
Members
448,967
Latest member
visheshkotha

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