Finding the first specific value above a certain cell

时间:2016-04-04 17:03:11

标签: excel excel-formula

I'm looking for a way to find the first specific value above a certain cell. Using the example below, in the result column every time I hit a A in COL2, I need to substract the value in COL1 to the first A value above.

The trouble seems finding a way to keep the range dynamic... I thought of =IF(B5="A";A5-INDIRECT("A"&MATCH("A";B:B;0));"")

But of course that only works for the first one as Match will always pick up the first one.

Any ideas? Thanks!

Example :

COL1    COL2    Result
1       A   
2       0
3       0
4       A       4 - 1 = 3
5       0
6       A       6 - 4 = 2
7       0
8       0
9       0
10      A       10 - 6 = 4

2 个答案:

答案 0 :(得分:0)

Try this:

=IF(AND(ROW(B2)<>2,B2="A"),A2-INDEX($A$1:$A1,AGGREGATE(14,6,ROW($1:1)/($B$1:$B1="A"),1)),"")

![enter image description here

The AGGREGATE() Function was introduced in 2010.

答案 1 :(得分:0)

  • In D2 enter =IF(B2="A",A2-INDIRECT("A"&MATCH("A"&(E2-1),F:F,0)),"")
  • In E2 enter =IF(B2="a",COUNTIFS($B$2:B2,"a"),"")
  • In F2 enter =B2&E2
  • Copy formulas from D, E and F down all your rows

Column D is the result you're looking for