How to sum pairs of numbers in an Excel column, even if separated by blank rows

I need to add up pairs of numbers, even when they are separated by blank rows.  My spreadsheet looks like this:
      A      B
1      ST      73.8
2      SB      -71.24
3      -      
4      -      
5      -      
6      -      
7      ST      83.63
8      -      
9      SB      -76.11
10      ST      82.25
11      SB      -77.3
12      ST      80.92
13      SB      -77.81
14      -      
15      -      
16      -      
17      ST      90.35
18      SB      -85.77

The labels stand for Swing Top and Swing Bottom, so adding one SB and one ST yields the net price move of that swing. I want to put a formula into column C that will add up each ST and SB pair and put the sum in the row next to the lower item in the pair, like this:

      A      B             C      
1      ST      73.8      
2      SB      -71.24      2.56
3      -            
4      -            
5      -            
6      -            
7      ST      83.63      12.39
8      -            
9      SB      -76.11      7.52
10      ST      82.25      6.14
11      SB      -77.3      4.95
12      ST      80.92      3.62
13      SB      -77.81      3.11
14      -            
15      -            
16      -            
17      ST      90.35      12.54
18      SB      -85.77      4.58

I need the net prices lined up this way, so that I can associate them with the date of the swing end, and also so I can perform additional study -- adding up different ranges of swings.

Thank you in advance for your help!!

wizzin
WizzinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Please post on Mondaymorning if you don't get an answer to this, as I'm away frommy computer at the moment.
0
Glenn RayExcel VBA DeveloperCommented:
Assuming your data starts in row 2, add this array formula (using [Ctrl]+[Shift]+[Enter]) in cell C3 and then copy down:
=IF(B3<>"",OFFSET($B$1,MAX(IF(ISBLANK($B$1:B2),0,ROW($1:2)))-1,0)+B3,"")

The MAX() function helps determine the previous swing value to add in order to calculate the delta.  

Example workbook attached.

Regards,
-Glenn
EE-Q28553191.xlsx
0
Martin LissOlder than dirtCommented:
put the sum in the row next to the lower item in the pair
That does not seem to be what you show in your example. Rather you seem to be showing the sum of a row and the row prior to it. In other words row 7 is 12.39 because that's the sum of -71.24 plus  83.63 and row 9 is 7.52 because that's the sum of 83.63 and  -76.11.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Glenn RayExcel VBA DeveloperCommented:
Martin,

I thought the questioner meant to insert the sum (the delta) adjacent to the second of each pair of numbers, regardless of how many rows there are between them.  That's what I did in my example and the results match his second example.

-Glenn
0
Martin LissOlder than dirtCommented:
Glenn, actually he says "put the sum in the row next to the lower item" by which I assumed he meant the lower value, but maybe I just don't understand what he means.
0
Angelo MiletoCommented:
I'm with Glenn. I put
=IF(ISBLANK(B2), "", SUM(OFFSET(B$1, MATCH(9.9E+307, B$1:B1, 1)-1, 0), B2))
in C2 and just filled down and matched his expected results.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WizzinAuthor Commented:
I did mean that the result should go next to the second (bottom) number in each pair. Sorry about the use of the ambiguous word lower.

Angelo's formula works, but puts the result one row below where it belongs. And I don't understand the ...9.9E+307...
I'm afraid that might not fit my actual spreadsheet, where I have lots of data and lots of columns.

Glenn's array formula seems great, except that my first ST or SB is actually in row 3 instead of Row 2. So I am assuming that in this case the formula should be:

=IF(B3<>"",OFFSET($B$1,MAX(IF(ISBLANK($B$1:B3),0,ROW($1:2)))-1,0)+B3,"")  

Is that correct?
0
WizzinAuthor Commented:
Actually, Glenn's formula did not produce the same results in my actual spreadsheet as in his example worksheet. In my spreadsheet, the values are naturally the result of formulas, rather than entered numbers. I don't know if that makes a difference.  Here's how my spreadsheet looks using Glenn's formula:

Swing      Value      
            
-            
-            
-            
ST      73.8           144.2
SB      -71.24      1.33
-            
-            
-            
-            
ST      83.63      161.9
-            
SB      -76.11      4.3
ST      82.25      160.09
SB      -77.3      4.95
ST      80.92      158.31
SB      -77.81      1.34
-            
-            
-            
ST      90.35      175.59
0
WizzinAuthor Commented:
Here's what happens when I use Angelo's formula:

Swing      Value      Delta
            
-            
-                            #N/A
-                            #N/A
ST      73.8                      #N/A
SB      -71.24              #N/A
-                              2.56
-                           -71.24
-                           -71.24
-                         -71.24
ST      83.63           -71.24
-                          12.39
SB      -76.11            83.63
ST      82.25              7.52
SB      -77.3              6.14
ST      80.92              4.95
SB      -77.81              3.62
-                            3.11
-                         -77.81
-                         -77.81
ST      90.35           -77.81
SB      -85.77            12.54
ST      92.58              4.58
SB      -87.3              6.81
-                            5.28
-                           -87.3
0
WizzinAuthor Commented:
I GOT IT!
Angelo's formula is the winner.  I had mistakenly pasted the first entry into the 3rd row instead of the 2nd. So that fixed the placement of the answer. Then, I simply fixed the first argument of the IF function to account for the fact that the value cell is not really blank, but contains "".

Here's the formula that worked:  
=IF(Q3="", "", SUM(OFFSET(Q$1, MATCH(9.9E+307, Q$1:Q2, 1)-1, 0), Q3))

Thank You!!
0
WizzinAuthor Commented:
Elegantly simple!
0
Angelo MiletoCommented:
Glad it worked. Should have considered they weren't blank. Glad you figured that out.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.