Keep Sum Positive Value until Negative Value appear

ibmy

Board Regular
Joined
Mar 4, 2020
Messages
134
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Does it possible to generate result in Column H?
From the past data, the max positive value appear (>=0) is below 100 row.
My data range between 250k-750k row.

6.8 - 12.xlsb
GH
1DataSum+
2-0.1
30
4-0.1
50.1
60.1
70.1
80
900.4
10-0.4
11-0.2
120
130
140.2
150.1
160.1
1700.4
18-0.1
190.1
200.1
210.1
220.2
230.1
240.1
250
260
270
280
290
300.1
310.1
320.2
330
340.1
350.2
360.2
370.1
380.1
390.1
400
410
420
430.1
440.2
450.1
460.2
470.1
480
490.1
500
510.1
520.1
530.33.2
54-0.1
550
560.2
5700.2
58-0.1
590.1
600.2
610.1
620.3
6300.7
64-0.2
Sheet1
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
MrExcel_Keep Sum Positive Value until Negative Value appear.xlsx
AB
1DataSum+
2-0,1 
300
4-0,1 
50,1 
60,1 
70,1 
80 
900,3
10-0,4 
11-0,2 
120 
130 
140,2 
150,1 
160,1 
1700,4
18-0,1 
190,1 
200,1 
210,1 
220,2 
230,1 
240,1 
250 
260 
270 
280 
290 
300,1 
310,1 
320,2 
330 
340,1 
350,2 
360,2 
370,1 
380,1 
390,1 
400 
410 
420 
430,1 
440,2 
450,1 
460,2 
470,1 
480 
490,1 
500 
510,1 
520,1 
530,33,2
54-0,1 
550 
560,2 
5700,2
58-0,1 
590,1 
600,2 
610,1 
620,3 
6300,7
64-0,2 
Sheet1
Cell Formulas
RangeFormula
B2:B64B2=IF(AND(NOT(ISBLANK(A2)),A2>=0,OR(A3<0,ISBLANK(A3))),SUM(INDEX(A$1:A2,1+MATCH(2,1/(A$1:A2<0),1)):A2),"")


For more than 10,000 rows you might prefer a VBA or a Power Query solution.
 
Upvote 0
Or try

Book1
GH
1DataSum+
2-0.1 
300
4-0.1 
50.1 
60.1 
70.1 
80 
900.3
10-0.4 
11-0.2 
120 
130 
140.2 
150.1 
160.1 
1700.4
18-0.1 
190.1 
200.1 
210.1 
220.2 
230.1 
240.1 
250 
260 
270 
280 
290 
300.1 
310.1 
320.2 
330 
340.1 
350.2 
360.2 
370.1 
380.1 
390.1 
400 
410 
420 
430.1 
440.2 
450.1 
460.2 
470.1 
480 
490.1 
500 
510.1 
520.1 
530.33.2
54-0.1 
550 
560.2 
5700.2
58-0.1 
590.1 
600.2 
610.1 
620.3 
6300.7
64-0.2 
Sheet1
Cell Formulas
RangeFormula
H2:H64H2=IF((G2>=0)*(G3<0),SUMIF($G$1:G2,">0")-SUM($H$1:H1),"")
 
Upvote 0
Give this a try

VBA Code:
Sub Pos_Sums()
  Dim a As Variant, b As Variant
  Dim i As Long
  Dim dSum As Double
  
  a = Range("G2", Range("G" & Rows.Count).End(xlUp).Offset(1)).Value
  a(UBound(a), 1) = -1
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a) - 1
    If a(i, 1) >= 0 Then
      If a(i + 1, 1) < 0 Then
        b(i, 1) = dSum
        dSum = 0
      Else
        dSum = dSum + a(i, 1)
      End If
    End If
  Next i
  Range("H2").Resize(UBound(b)).Value = b
End Sub
 
Upvote 0
Thanks @Peter_SSs
The code works fast on 250-750k row data
Some result correct, some dont
Results which do not correct, just missing about 0.1-0.3 point
Mostly the result are work

Example of result that do work is on Row 53
 
Upvote 0
A little edit:

VBA Code:
Sub Pos_Sums()
  Dim a As Variant, b As Variant
  Dim i As Long
  Dim dSum As Double
 
  a = Range("G2", Range("G" & Rows.Count).End(xlUp).Offset(1)).Value
  a(UBound(a), 1) = -1
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a) - 1
    If a(i, 1) >= 0 Then
    dSum = dSum + a(i, 1)
      If a(i + 1, 1) < 0 Then
        b(i, 1) = dSum
        dSum = 0
      End If
    End If
  Next i
  Range("H2").Resize(UBound(b)).Value = b
End Sub
 
Upvote 0
Solution
Thanks @Phuoc for modify the code, the code working great
Also thanks to @Peter_SSs , original coder

May God bless all of you
 
Upvote 0
Oops, Yes I did have my sum in the wrong place. :oops:
Thanks to @Phuoc for tidying that up. (y)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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