How do I restart a total if a cell equals 0?
I am trying to automatically reset my total if a cell equals 0.
A1 9
B1 9
C1 0 <-- Reset total
D1 9
E1 9
F1 9
G1 9
H1 36 (Should = 36, resetting at any cell showing 0 and not show 54)
Fields A1:G1
hold the daily hours worked on the roster, while H1
contains the total hours worked that week. (However, I want to reset this total if there is a 0 hour rest day)
Is there a formula that can do this?
We can use a "helper" row. In A2 enter: =A1
in B2 enter:
=IF(B1=0,0,B1+A2)
and copy across. Finally in H1 enter: =G2
Wih BVA / Macros
Public Function sumreset(dataRange As Range)
totalSum = 0
totalLength = -1
indicator = "none"
totalLengthRows = dataRange.Rows.Count
totalLengthCols = dataRange.Columns.Count
If (totalLengthRows = 1) And (totalLengthCols > 1) Then
totalLength = totalLengthCols
indicator = "cols"
ElseIf (totalLengthRows > 1) And (totalLengthCols = 1) Then
totalLength = totalLengthRows
indicator = "rows"
End If
For i = totalLength To 1 Step -1
Select Case indicator
Case "rows"
nextData = dataRange(i, 1)
Case "cols"
nextData = dataRange(1, i)
End Select
If nextData <> 0 Then
totalSum = totalSum + nextData
Else
i = 1
End If
Next i
sumreset = totalSum
End Function
Open VBA / Macros with ALT+F11, under ThisWorkbook insert a module and paste this code on the right side.
On cell H1 put =sumreset($A$1:$G$1)
and that's all.