sum the consecutive

ibrahimo00o

New Member
Joined
Jun 26, 2011
Messages
8
sum data in same Column of consecutive positive and negative in excel

like this

2
6
-2
1
3
-5
-5
-1
9

i need to transform it to

8
-2
4
-11
9

note that may be found more than 3 values consecutive positive or negative
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Assuming that Column A, starting A2, contains the data, try...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()

    [color=darkblue]Dim[/color] Cell [color=darkblue]As[/color] Range
    
    Application.ScreenUpdating = [color=darkblue]False[/color]

    [color=darkblue]Set[/color] Cell = Range("A2")
    
    [color=darkblue]Do[/color] [color=darkblue]While[/color] Cell.Value <> ""
        [color=darkblue]If[/color] Sgn(Cell) = Sgn(Cell.Offset(1, 0)) [color=darkblue]Then[/color]
            Cell.Value = Cell.Value + Cell.Offset(1, 0).Value
            Cell.Offset(1, 0).EntireRow.Delete
        [color=darkblue]Else[/color]
            [color=darkblue]Set[/color] Cell = Cell.Offset(1, 0)
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Loop[/color]
    
    Application.ScreenUpdating = [color=darkblue]True[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0
Hi

Are zeroes allowed? In that case what should be the result of:

2
0
3
1
0
0
-5
-1
3
0
 
Upvote 0
ibrahimo00o,

Actually, there's a flaw in my code. Try the following instead..

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()

    [color=darkblue]Dim[/color] LastRow [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]
    
    Application.ScreenUpdating = [color=darkblue]False[/color]
    
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    [color=darkblue]For[/color] i = LastRow [color=darkblue]To[/color] 2 [color=darkblue]Step[/color] -1
        [color=darkblue]If[/color] Sgn(Cells(i, "A")) = Sgn(Cells(i - 1, "A")) [color=darkblue]Then[/color]
            Cells(i - 1, "A").Value = Cells(i - 1, "A").Value + Cells(i, "A").Value
            Rows(i).Delete
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] i
    
    Application.ScreenUpdating = [color=darkblue]True[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]

pgc01,

Good point... Based on your example, my macro will return the following results...


2
0
4
0
-6
3
0

If this is not the desired result, the code will need to be amended.
 
Upvote 0
ibrahimo00o,

Actually, there's a flaw in my code. Try the following instead..

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()

    [color=darkblue]Dim[/color] LastRow [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]
    
    Application.ScreenUpdating = [color=darkblue]False[/color]
    
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    [color=darkblue]For[/color] i = LastRow [color=darkblue]To[/color] 2 [color=darkblue]Step[/color] -1
        [color=darkblue]If[/color] Sgn(Cells(i, "A")) = Sgn(Cells(i - 1, "A")) [color=darkblue]Then[/color]
            Cells(i - 1, "A").Value = Cells(i - 1, "A").Value + Cells(i, "A").Value
            Rows(i).Delete
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] i
    
    Application.ScreenUpdating = [color=darkblue]True[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]

pgc01,

Good point... Based on your example, my macro will return the following results...


2
0
4
0
-6
3
0

If this is not the desired result, the code will need to be amended.

its work.... great thank u very much :)

i was tired to solve this prob.........

but now every things okay.................;) thx
 
Upvote 0
I NEED TO COUNT consecutive positive or negative VALUES IN SECOND Column IN THE SAME CODE IN THE ABOVE LIKE THIS EXAMPLE

2
6
-2
1
3
-5
-5
-1
9

i need to transform it to



C ___________D

8____________2
-2___________1
4____________2
-11__________3
9____________1
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

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