简体   繁体   中英

Excel find last match from two tables

I'm looking for a way to find the last instance on an entry in the columns A,B and get the corresponding values from columns C,D

In the example below value for Henry is 1374 and value for Amy is 1124

例

Name1 corresponds to Value1 and Name2 corresponds to Value2. Is there a formula to find the last entry from both columns Name1 and Name2 and return the corresponding Value1 or Value2

Raw data pasted below:

Name1   Name2   Value1  Value2
Sara    Amy     1265    1241
John    Sara    1142    1214
Amy     Henry   1295    1121
Amy     John    1175    1323
Sara    John    1085    1251
Sara    Henry   1242    1374
Amy     Sara    1124    1055

Assumptions:

Data Grid is in cells A1:D8 . Values "Henry" & "Amy" are in cell A10 & A11 respectively.

Formula implementation:

In cell B10 following formula is implemented.

Alternative 1:

=INDEX($C$2:$D$8,MAX(IFERROR(LOOKUP(2,1/($A$2:$A$8=A10),ROW($A$2:$A$8)),-1),IFERROR(LOOKUP(2,1/($B$2:$B$8=A10),ROW($B$2:$B$8)),-1))-1,IF(IFERROR(LOOKUP(2,1/($A$2:$A$8=A10),ROW($A$2:$A$8)),-1)>IFERROR(LOOKUP(2,1/($B$2:$B$8=A10),ROW($B$2:$B$8)),-1),1,2))

Alternative 2 (slightly shorter than 1):

=INDEX($C$2:$D$8,LOOKUP(2,1/SEARCH(A10&",",$A$2:$A$8&","&$B$2:$B$8&",",1),ROW($A$2:$A$8))-1,IF(IFERROR(LOOKUP(2,1/($A$2:$A$8=A10),ROW($A$2:$A$8)),-1)>IFERROR(LOOKUP(2,1/($B$2:$B$8=A10),ROW($B$2:$B$8)),-1),1,2)).

To be copied down as much needed.

Notice -1 value after MAX() function which is used to adjust row number. It should be always n-1 considering data starts at nth row.

Slightly shorter:

= INDEX($C$1:$D$8,MAX(IF($A$1:$B$8=A10,ROW($A$1:$B$8))),MATCH(A10,
  INDEX($A$1:$B$8,MAX(IF($A$1:$B$8=A10,ROW($A$1:$B$8))),0),0))

Note this is an array formula, so you must press Ctrl + Shift + Enter rather than just Enter after typing the formula.

See below for working example.

在此输入图像描述

I've been trying to remember another method I've seen for 2d lookup (I can't find the link any more). It's basically like this

=INDIRECT(TEXT(MAX((ROW($A$2:$B$8)*100+COLUMN($A$2:$B$8))*($A$2:$B$8=A10))+2,"R0C00"),FALSE)

entered as an array formula using Ctrl Shift Enter .

So the idea is that you generate a number from the row and column where the last occurrence of the name is located (so for Henry it would be 702).

The you format it to give R7C02 and feed this in to an indirect to give the reference to the cell in RC notation. The column plus 2 gives the cell that you want.

You might notice that this would fail if column>99, but you can make the multiplier as big as you want.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM