暂时没想到直接的办法,加一个辅助列:
B2=IF(A2="",B1,IF(A2>0,1,0))
C2=IF(OR(AND(B2=0,B3=0),AND(B2>0,B3>0)),"",SUM(A$2:A2)-SUM(C$1:C1))
公式向下复制到红色单元格(只能复制到A列最后一个非空单元格所在的行)
复杂的问题不是可以用一个公式能解决的,就要用VBA处理,我写个程序,你运行试试:
Sub 分正负累加()
lastrow = Cells(Rows.Count, 1).End(3).Row
i = 2
s = 0
c = Cells(i, 1)
Do While i <= lastrow
If Cells(i + 1, 1) <> "" Then
If c * Cells(i + 1, 1) < 0 Then
s = s + Cells(i, 1)
Cells(i, 2) = s
s = 0
c = Cells(i + 1, 1)
Else
s = s + Cells(i, 1)
End If
Else
If Cells(i, 1) <> "" Then
s = s + Cells(i, 1)
End If
End If
i = i + 1
Loop
i = i - 1
Cells(i, 2) = s
End Sub