Sum If Formula

jthompson

Board Regular
Joined
Aug 27, 2002
Messages
57
Office Version
  1. 365
Platform
  1. Windows
I am trying to have the cell in column I look at column G. If that cell has a 0 I want to sum the number above up stopping at the next zero above. For instance cell G 7 has a zero. I7 needs to sum G2:G6. I have this somewhat working but the sum is appearing in cell I1. The sum is i7 is actually summing G8:G9 this is correct but that sum needs to be in I10. Basically its putting the sum above rather than below.

=IF(G7=0,SUM(INDIRECT("G"&ROW()+1&":G"&IFERROR(MATCH(0,INDIRECT("G"&ROW()+1&":G"&ROW()+100),0)+ROW()-1,ROW()+100))),"0")
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
@jthompson
Would you be kind enough to post a mini worksheet using the xl2bb add in (Link below)
And also, please tell us what version of excel you use, and update your profile so the version you use shows up on your thread button.

Solutions may differ from version to version. And a better solution may be available by seeing what your data is like and getting an idea of the objection.
I am loathe to use INDIRECT or OFFSET in any bulk formula as they can affect processing time substantially.

If for some reason you are unable to use xl2bb, please paste a TABLE and not an image. Images require that the forum completely recreate your scenario and that WILL take time and can possibly have errors by typos. If you must post a table please identify columns and rows, give formulas in the text (and identify the cell(s)). And if at all possible expected values from the calculations.

Thanks in advance.
 
Upvote 0
Thanks for updating your version details. (y)

See if this is what you want.

23 05 05.xlsm
GHI
1
22 
33 
45 
51 
64 
7015
83 
96 
1009
jthompson
Cell Formulas
RangeFormula
I2:I10I2=IF(G2=0,SUM(G$2:G2)-SUM(I$1:I1),"")
 
Upvote 0
Solution
You're welcome. Thanks for the follow-up. :)
 
Upvote 0
@jthompson , thanks for this question. I've needed something like this for quite a while and never sat down to figure it out.
Glad @Peter_SSs answered before I did. The simplicity is lovely thus far better than what I came up with.


I made a complicated formula starting with yours and using the Aggregate function to sum numbers back to the immediate prior zero. (Your formula worked summing from the top, as it found the first occurrence of zero below the current row. Couldn't use the same formula going up as it would always look to the first zero in the column)

=IF(G7=0, SUM(INDIRECT("G"&ROW()-1&":G"&IFERROR(MATCH(0,INDIRECT("G"&ROW()-1&":G"&(AGGREGATE(15,6,ROW($1:7)/($G$1:$G7=0),COUNTIF($G$1:$G7,0)))),0)+AGGREGATE(15,6,ROW($1:7)/($G$1:$G7=0),COUNTIF($G$1:$G7,0)),AGGREGATE(15,6,ROW($1:7)/($G$1:$G7=0),COUNTIF($G$1:$G7,0))))) ,"")
1683308432098.png
 
Upvote 0
I made a complicated formula ..
You certainly did! :eek:
Welcome to the MrExcel board!

If you did want to actually look upwards for the previous zero and just sum that range rather than the way I did it above, then you could still avoid such a complicated formula (and multiple uses of the volatile function INDIRECT) as follows:

23 05 05.xlsm
GHI
1
22 
302
45 
51 
64 
7010
83 
96 
1009
jthompson (2)
Cell Formulas
RangeFormula
I2:I10I2=IF(G2=0,SUM(XLOOKUP(0,G$1:G1,G$1:G1,G$1,,-1):G1),"")
 
Upvote 1
@Peter_SSs , once again, Wow!
RangeFormula
Cell Formulas
I2:I10I2=IF(G2=0,SUM(XLOOKUP(0,G$1:G1,G$1:G1,G$1,,-1):G1),"")
That's the one I'll be using! I'll sometimes use formula in a column that has other numbers, so the simple subtraction method won't work in all cases for me.
Agreed about using the Indirect formula -- it's unweildy and hard to troubleshoot. Using a butter knife when I don't have a screwdriver handy.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

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