Suming numbers until a positive number is reached

cjw6886

New Member
Joined
Nov 5, 2016
Messages
2
Hello,

I need a formula to reach the output below. I need to have the cells sum the numbers before it that are negative and have it stop once it reaches a positive number. Also, if the number is negative I need it to end up as 0. Thanks in advance for any contribution you make to helping me get this figured out!


Data
200 400 -100 -300 500 600 -200 700

Output
200 400 0 0 100 600 0 500
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

<colgroup><col style="width:48pt" width="64" span="8"> </colgroup><tbody>


</tbody>
200
400-100-300500600-200700
200
400002006000500

<tbody>
</tbody>

highlight select your target range manually , then run the below code,

Code:
Sub Shreif11Nov2016()
    Dim rng, rng1 As Range
    Dim i, c As Long
    For Each rng In Selection
        If rng > 0 Then
            rng.Offset(1) = rng
        Else
            rng.Offset(1) = 0
      End If
    Next
    Selection.Offset(1).Select
    For Each rng1 In Selection
        If i < 0 And rng1 <> 0 Then
            rng1 = rng1 + c
            i = 0
            c = 0
        End If
        If rng1 = 0 Then
            i = rng1.Offset(-1)
            c = c + i
        End If
    Next
End Sub
 
Upvote 0
Try,

1] Left A1 blank, your data housed in B1:I1

2] In B2, formula copy across :

=IF(B1>0,IF(A1<0,MIN(A1+B1,B1),B1),0)

Regards
 
Upvote 0
Hi,

Output as your example :

1] Left A1 blank, your data housed in B1:I1

2] In B2, formula copy across :

=IF(B1<0,0,IF(A1<0,SUM($B$1:B1)-SUM(A$2:$B2),B1))
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,547
Members
449,089
Latest member
davidcom

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