?
Solved

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

Posted on 2014-11-07
12
Medium Priority
?
559 Views
Last Modified: 2014-11-08
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
0
Comment
Question by:Wizzin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +2
12 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40429454
Please post on Mondaymorning if you don't get an answer to this, as I'm away frommy computer at the moment.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40429555
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
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40429620
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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40429641
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
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40429698
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
 
LVL 1

Accepted Solution

by:
Angelo Mileto earned 2000 total points
ID: 40429719
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
 

Author Comment

by:Wizzin
ID: 40430191
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
 

Author Comment

by:Wizzin
ID: 40430221
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
 

Author Comment

by:Wizzin
ID: 40430226
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
 

Author Comment

by:Wizzin
ID: 40430244
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
 

Author Closing Comment

by:Wizzin
ID: 40430245
Elegantly simple!
0
 
LVL 1

Expert Comment

by:Angelo Mileto
ID: 40430593
Glad it worked. Should have considered they weren't blank. Glad you figured that out.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question