Hi everyone
Is it possible to SUM all cells after a particulat number (in this case zero)?
for instance
10
8
7
6
0
0
2
4
6
We would get 12.
Many thanks
Stu
=SUM(OFFSET(INDEX(A:A,MATCH(BigNum,A:A)),0,0,
-(MATCH(BigNum,A:A)-MATCH(BigNum,1/(A2:INDEX(A:A,MATCH(BigNum,A:A))=0)))))
=MATCH(BigNum,A:A)
=SUM(OFFSET(INDEX(A:A,B2),0,0,-(B2-MATCH(BigNum,1/(A2:INDEX(A:A,BigNum)=0)))))
Excel Workbook | |||||
---|---|---|---|---|---|
A | B | C | |||
1 | 10 | 0 | 8 | ||
2 | 1 | ||||
3 | 7 | ||||
4 | 1 | ||||
5 | 0 | ||||
6 | 0 | ||||
7 | 2 | ||||
8 | |||||
9 | 6 | ||||
Sheet1 |
Option Explicit
Sum since zero ()
' akinrotimi, 14/08/2011
http://www.mrexcel.com/forum/newreply.php?do=newreply&noquote=1&p=2735061
Application.ScreenUpdating = False
Range("AA1").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-26]=0,"""",RC[-26])"
Selection.AutoFill Destination:=Range("AA1:AA3000"), Type:=xlFillDefault
Range("Ab1").Select
ActiveCell.FormulaR1C1 = _
"=IF(IF(RC[-1]="""",""q"","""")=""q"",SUM(RC[-1]:R[10000]C[-1]),"""")"
Selection.AutoFill Destination:=Range("AB1:AB10000")
Range("AB1:AB10000").Select
Columns("AB:AB").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.TextToColumns Destination:=Range("AB1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(0, 1), TrailingMinusNumbers:=True
Range("ab1").End(xlDown).Offset(0, 0).Select
Selection.Copy
Range("a1000000").End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
Columns("AA:AC").Select
Selection.ClearContents
Range("a1000000").End(xlUp).Offset(0, 0).Select
Application.ScreenUpdating = True
Application.CutCopyMode = False
Range("a1000000").End(xlUp).Offset(0, 0).Select
Application.ScreenUpdating = True
RotimiThe code below solves the problem:
=SUM(INDEX(A2:A10,MATCH(2,1/(A2:A10=0),1)+1):A10)
Rotimi
You did appear to ask for some feedback on your code, so ..
Fair enough. If that assumption is correct then that would certainly mean that your could would not return an incorrect result for the first example I suggested.3.Row 1 is meant to be the header row without data,so we should not expect any data there.
I'm not sure what you mean by speed is not affected.4.The run time of my code is 1.68 second so speed is not affected.
You point out that my code only sums the values below the last zero and suggest that this was not the OPs intention. I don't understand how you would conclude it was not the OP's intention when the thread title is "SUM since last zero", which you are confirming my code does, not "SUM since first zero", which your code does.The code works perfectly under all conditions within the set range of 3,000 rows I used(whether there are empty cells or hundreds of zeros within the set range provided).
I also checked your solution and notice that it only sums up values below the last zero and this was not the OPs intention since the example provided even had multiple zeros.
My code solves the OP's issue perfectly.
Hi everyone
Many thanks, I went with Peters and it works a treat.