Avatar of Wizzin
Wizzin
 asked on

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
Microsoft ApplicationsMicrosoft Excel

Avatar of undefined
Last Comment
Angelo Mileto

8/22/2022 - Mon
Phillip Burton

Please post on Mondaymorning if you don't get an answer to this, as I'm away frommy computer at the moment.
Glenn Ray

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
Martin Liss

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Glenn Ray

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
Martin Liss

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.
ASKER CERTIFIED SOLUTION
Angelo Mileto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Wizzin

ASKER
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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Wizzin

ASKER
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
Wizzin

ASKER
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
Wizzin

ASKER
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!!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Wizzin

ASKER
Elegantly simple!
Angelo Mileto

Glad it worked. Should have considered they weren't blank. Glad you figured that out.