INDEX and MATCH in Excel: the lookup that works in both directions
VLOOKUP works until the day you need a value that sits to the left of the key column. Then it stops, and no amount of dragging fixes it. INDEX and MATCH together do the same job in any direction, and they survive someone inserting a column into your table. This guide is the pattern, the two-way version, and the errors that cost the most time.
Why VLOOKUP hits a wall
VLOOKUP takes a lookup value, a table, a column number, and a match mode. The column number is the problem. It counts from the first column of the range you selected, so the return column must always be to the right of the key. If the key is in column C and the value you want is in column A, VLOOKUP cannot reach it without rearranging the sheet.
The second problem is quieter. Insert a column inside the lookup range and every column number shifts. The formula does not break with an error. It returns the wrong column, which is worse, because nobody notices until a report is wrong.
INDEX and MATCH avoid both. INDEX takes a range and a position, so it does not care where the range sits. MATCH finds the position, so it does not care about column order either.
The two functions, separately
MATCH answers: at what position in this list does this value appear?
=MATCH("Mona Adel",A2:A9,0) returns 1 if Mona Adel is the first name in A2:A9. The third argument is the match type. Zero means exact match, and for lookups you almost always want zero. One means approximate, ascending, which is how tax brackets and grade bands are matched. Minus one means approximate, descending.
INDEX answers: what is the value at this position in this range?
=INDEX(C2:C9,3) returns the third value in C2:C9. Give INDEX a single column and a row number, and it returns one cell. Give it a rectangle and two numbers, and it returns the cell at that row and column inside the rectangle.
Alone, neither is impressive. MATCH gives you a position you cannot use directly. INDEX needs a position you do not know. Together they close the loop.
The pattern
=INDEX(return_range,MATCH(lookup_value,key_range,0))
Read it from the inside out. MATCH finds where the lookup value sits in the key column. INDEX takes that number and returns the value at the same position in a different column. The two ranges must be the same height, because the position has to mean the same row in both.
That is the whole technique. Everything else in this guide is variations on it.
A worked example
The sheet below has a staff table in A1:D9 and a small lookup block in F1:G7. The block asks for a name, then returns the region and the salary for that name.
The region formula in G2 is =INDEX($C$2:$C$9,MATCH($G$1,$A$2:$A$9,0)). The salary formula in G3 is the same shape with a different return range: =INDEX($D$2:$D$9,MATCH($G$1,$A$2:$A$9,0)).
Change the name in G1 and both values update. No column numbers to count, and the key column can sit anywhere.
The left lookup VLOOKUP cannot do
The second block asks the opposite question: given a salary, which department is that person in? The key is column D and the answer is column B, which is to the left.
=INDEX($B$2:$B$9,MATCH($G$6,$D$2:$D$9,0)) returns Finance for 14500. VLOOKUP would need the salary column moved to the left of the department column, which changes the sheet for everyone else.
This is the case that makes people learn INDEX and MATCH in the first place, and it comes up constantly with exported data, where the identifier sits in the middle of the columns rather than at the start.
Two-way lookup
When the row and the column both vary, use two MATCH calls inside one INDEX:
=INDEX($B$2:$M$40,MATCH($A45,$A$2:$A$40,0),MATCH(B$44,$B$1:$M$1,0))
The first MATCH finds the row, the second finds the column, and INDEX returns the cell where they meet. The mixed references matter: the row key is locked to its column with $A45, and the column header is locked to its row with B$44. That way one formula can be dragged across and down to fill a whole grid.
This is how a monthly table becomes a lookup engine: names down the side, months across the top, one formula in the corner, dragged.
Wildcards and approximate matches
MATCH accepts the same wildcards as COUNTIF. =MATCH("Cai*",C2:C9,0) finds the first entry starting with Cai. The asterisk covers any number of characters, the question mark covers exactly one. Wildcards only work on text, and only with match type zero.
Approximate matching is the other mode. =MATCH(78,$F$2:$F$9,1) with an ascending list of grade boundaries returns the position of the largest value that is less than or equal to 78. That is exactly how banded pricing, tax tables and grading work. Sort the band list ascending first, or the answer is silently wrong.
The errors, in the order they happen
#N/A means MATCH found nothing. Either the value is not there, or it is there in a different type. A number stored as text will not match the same digits stored as a number, and a trailing space will not match a clean string. Wrap the lookup value in TRIM, or clean the key column once with Data, Text to Columns.
#REF! usually means the INDEX range is shorter than the position MATCH returned. The two ranges must be the same height. If MATCH looks at A2:A9 and INDEX at C2:C8, position 8 points outside the return range.
A wrong answer with no error is the dangerous one. It comes from match type 1 on an unsorted list, or from ranges of different heights where the shift happens to stay inside the range. Check the two ranges have the same number of rows before you trust the output.
Slow recalculation comes from whole-column references. MATCH(A2,A:A,0) looks at a million rows. Limit the range to the data, or use a table, and a workbook that took seconds to recalculate returns to instant.
INDEX and MATCH versus XLOOKUP
XLOOKUP replaces both functions with one and does it well: no column numbers, native left lookups, a built-in not-found argument, and a default of exact match. If everyone opening the file has Excel 2021 or Microsoft 365, use XLOOKUP and skip this pattern.
INDEX and MATCH still earn their place in three situations. The file has to open in older Excel or in Google Sheets, where XLOOKUP is not available everywhere. The workbook is shared with people on Excel 2019. Or the formula has to work identically in both Excel and Google Sheets without a compatibility note. In those cases the two-function pattern is the portable choice.
One more: INDEX with two MATCHes does a genuine two-way lookup in one formula. XLOOKUP can nest inside itself to do the same, but the grid version is easier to read and to audit.
Naming the ranges
Long formulas are hard to audit. Name the key column once, and the formula reads like a sentence: =INDEX(Salary,MATCH($G$1,Employee,0)). A named range also survives rows being inserted above it, which a hard-coded reference does not. Keep the names short and specific, one name per column, and write them down on a notes sheet if more than one person edits the file.
The checklist before you ship it
- Both ranges are the same height. Count them, do not assume.
- The key column has no trailing spaces and no numbers stored as text.
- Match type is 0 for exact lookups, 1 only on a sorted ascending band list.
- Ranges are absolute with $ if the formula will be dragged.
- One row in the test data has a value that is not in the key list, so you can see the #N/A case behaves the way you want.
Five checks, about a minute, and they catch nearly every broken lookup I have seen in a shared workbook.
Lookup on two conditions
MATCH takes one lookup value, so two conditions need a workaround. The cleanest one is a helper column that joins the keys:
=A2&"|"&B2
Put that in a spare column, fill it down, then match against it with the same join on the lookup side: =INDEX($D$2:$D$9,MATCH($G$1&"|"&$G$2,$E$2:$E$9,0)). The pipe character matters less than the habit of picking a separator that does not appear inside the data. A space or a comma can collide with real values and produce a match that looks right and is not.
The array version exists too, using MATCH inside an array formula that multiplies two conditions together. It works, it is hard to read a month later, and it is slower on large sheets. The helper column is easier to audit and easy to hide.
Duplicates and the first-match rule
MATCH stops at the first hit. If the key column holds duplicates, every lookup returns the first row and the rest are invisible. That is fine when the duplicates are identical. It is a silent bug when they are not, for example an order number that appears twice because the order was amended.
Two defences: check the key column once with COUNTIF to see whether any key appears more than once, and when it does, decide which row should win before the lookup goes into the sheet. A lookup that returns the first match is a rule, not a random result, and the sheet should say so somewhere a reader can see.
Frequently asked questions
Can INDEX and MATCH look up to the left?
Yes. That is the main reason people switch from VLOOKUP. INDEX takes a return range that can sit anywhere on the sheet, and MATCH finds the position in the key column independently.
Is INDEX and MATCH faster than VLOOKUP?
On exact matches over large ranges, usually yes, because MATCH stops at the first hit while VLOOKUP can scan the whole table. On small tables the difference is not measurable. Whole-column references slow both down.
Why does MATCH return #N/A when I can see the value?
Almost always a type mismatch: the lookup value is text and the key column holds numbers, or one side has a trailing space. TRIM the key column and check the cell format of both sides.
Does this work in Google Sheets?
Yes, with the same syntax. INDEX, MATCH and the two-way pattern behave the same in Google Sheets as in Excel, which is why some teams standardise on it over XLOOKUP.
Can I return multiple columns at once?
INDEX returns one value per formula. To return a whole row, either copy the formula across with the return range fixed and the column reference moving, or use FILTER in newer versions.
Related guides
If you are choosing between the two lookup functions, the comparison of XLOOKUP and VLOOKUP covers where each one breaks. For counting and summing by a condition, see COUNTIF and SUMIF. And if the table you are looking up in needs rebuilding first, the pivot table tutorial shows how to turn raw rows into something a lookup can trust.
Comments
Post a Comment