4
votes

I have a spreadsheet with 2 columns of data, column A and B, and column C where I'm looking for the formula.

row    A      B       C
 1     50
 2   
 3   
 4   
 5            56      6
 6   
 7   
 8     46            10
 9   
 10   
 11           64     18

As you can see, a row either contains a value or not. In column C, I'm looking to calculate the difference between

a) the value in column B and the value in column A's first non-empty cell directly above (so for example, at row 5, I calculate the difference between B5 and A1 56 - 50 = 6) if the value of B is a number and

b) the value in column A and the value in column B's first non-empty cell directly above (row 8, 56 - 46 = 10)

and put nothing if neither column A and B are empty.

I've been struggling with "first non-empty cell" to write an R1C1 formula. Note that I know I can do this in VBA but I'm looking for the R1C1 formula.

Thanks for your help.

2

2 Answers

4
votes

Here is an array formula (that you need to validate with Ctrl + Shift + Enter) you can put in C1 and drag and drop till the end of your data:

=IF(OR(A1<>"",B1<>""),INDEX($B$1:B1,MAX(IF($B$1:B1="",0,ROW($B$1:B1))))-INDEX($A$1:A1,MAX(IF($A$1:A1="",0,ROW($A$1:A1)))),"")

Or, in a french version of Excel:

=SI(OU(A1<>"";B1<>"");INDEX($B$1:B1;MAX(SI($B$1:B1="";0;LIGNE($B$1:B1))))-INDEX($A$1:A1;MAX(SI($A$1:A1="";0;LIGNE($A$1:A1))));"")

Note that if you feel interested, you can commit into Stackoverflow in french

3
votes

Perhaps try this formula in C2 copied down

=IF(B2="",IF(A2="","",LOOKUP(9.99E+307,B$1:B1)-A2),B2-LOOKUP(9.99E+307,A$1:A1))