Yamaha32088
Yamaha32088

Reputation: 4163

Excel, use value returned from function to select the corresponding cell

I have tried searching for the answer to this so if it has already been answered then I must not be using the correct terminology.

Basically I have three columns and about 700 rows. What I need to accomplish is:

+---------+---------+-----+
|    A    |    B    |  C  |
+---------+---------+-----+
| chicken | beef    | 112 |
| pork    | chicken | 541 |
| beef    | pork    | 312 |
| salami  |         |     |
+---------+---------+-----+

I need a formula that searches the current cell in cell A for a match in the entire B column. Upon finding the match it needs to return the value in the matched cells row number at C.

So for example I am using the formula =MATCH(A1, B$1:B$4,0) which will return the current position of the matched value. So the cell with the formula would return the number 2. Now what I need to figure out is how I can get the value at C2?

So in somewhat psuedo code:

If A1 matches B2 get value at C2

If A2 matches A2 get value at C3

I would like to have a function like:

=IF(MATCH(A1,B$1:B$4,0),C*numberReturnedHere*,"No Matches Found")

Hopefully I have provided enough information and made it clear what I would like to do. Thanks

EDIT:

Desired Result

+---------+---------+-----+------------------+
|    A    |    B    |  C  |        D         |
+---------+---------+-----+------------------+
| chicken | beef    | 112 | 541              |
| pork    | chicken | 541 | 312              |
| beef    | pork    | 312 | 112              |
| salami  |         |     | No Matches Found |
+---------+---------+-----+------------------+

Upvotes: 0

Views: 315

Answers (2)

Grade 'Eh' Bacon
Grade 'Eh' Bacon

Reputation: 3823

This is the INDEX function. Index works by taking a single column, a single range, or a 2D table of data, and it returns the row and column number you specify. Because you only want to return data from column C, we can ignore assigning a column. With your MATCH function (which is the ideal partner of INDEX), the formula would look like this:

=INDEX(C:C,MATCH(B:B,A1,0))

Alternatively, you could do this with a single VLOOKUP function, which looks at the left-most column of a data table, finds a matching value, and returns the value from that row, at a specified number of columns in. This would look like:

=VLOOKUP(A1,B:C,2,0)

Note that while this is a little simpler, it will be useful for you to regularly use INDEX + MATCH, as that is more versatile.

As @Gary points out, you can wrap either formula with IFERROR to get your desired error message if no match is found.

Upvotes: 1

Gary's Student
Gary's Student

Reputation: 96753

In D1 enter:

=IFERROR(VLOOKUP(A1,$B$1:$C$3,2,FALSE),"NO MATCH FOUND")

and copy down.

enter image description here

Upvotes: 1

Related Questions