MSBA-175 · Lesson 6 of 12 · 24 min
Lookup functions: XLOOKUP and INDEX/MATCH
▶ Video coming soon A video version of this lesson is on the way.
Every Maple & Pine workbook looks something up. The quote sheet finds a package price from its code, the bookings sheet finds a venue's capacity, and the commission sheet finds a coordinator's rate band. For years these used VLOOKUP, and in October one of them broke when someone inserted a column into the venue table: every capacity shifted one column to the left and showed the venue's postal code instead. Dagny's sixth session replaced those formulas and covered the exam objective look up data by using the XLOOKUP(), VLOOKUP(), HLOOKUP(), MATCH(), and INDEX() functions. All five are on the exam, so even though XLOOKUP is the modern choice, you need to read and write the older ones too.
VLOOKUP and HLOOKUP
VLOOKUP searches the first column of a table for a value and returns a value from a column you specify by number. HLOOKUP does the same across the first row of a table and returns from a row number. Both take four arguments.
=VLOOKUP(E5, Packages, 3, FALSE)
=HLOOKUP("Q2", Lists!$B$1:$E$4, 3, FALSE)| Argument | Meaning | Trap |
|---|---|---|
| lookup_value | The value to find | Must be in the first column (or row) of the table |
| table_array | The range to search | Make it absolute or use a table or name |
| col_index_num / row_index_num | Which column or row to return, counting from 1 | Breaks when columns or rows are inserted |
| range_lookup | FALSE for exact match; TRUE or omitted for approximate | Omitting it means approximate match, which needs sorted data |
Two limitations explain why VLOOKUP causes so much trouble. It can only look to the right of the search column, and its column number is a fixed count that does not move when the table changes. The October failure was exactly that: the formula returned column 4, and after a column was inserted, column 4 was a different field. The fourth argument is the other classic error: leave it out and VLOOKUP does an approximate match, returning a plausible but wrong row for any code it cannot find exactly.
Approximate match has a proper use: banded lookups. Commission rate bands are listed with their lower limits in ascending order, and VLOOKUP with TRUE finds the largest limit that is less than or equal to the value. That is the correct way to find which band a number falls in.
=VLOOKUP(G5, Rates, 2, TRUE)XLOOKUP
XLOOKUP replaces both VLOOKUP and HLOOKUP. Instead of a table and a column number, you give it a lookup array and a return array, so it can look left or right, up or down, and it does not break when columns are inserted. It defaults to exact match, and it has a built-in value for "not found".
=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])Dagny's capacity formula became =XLOOKUP(C5, Venues[Name], Venues[Capacity], "Not found"). With structured references, inserting a column into the Venues table has no effect on it. The if_not_found argument replaces the IFERROR or IFNA wrapper older formulas needed.
| match_mode | Meaning |
|---|---|
| 0 (default) | Exact match; #N/A or if_not_found when missing |
| -1 | Exact match, or the next smaller item |
| 1 | Exact match, or the next larger item |
| 2 | Wildcard match using an asterisk, a question mark and a tilde |
| search_mode | Meaning |
|---|---|
| 1 (default) | Search from first to last |
| -1 | Search from last to first (find the most recent entry) |
| 2 | Binary search on data sorted ascending |
| -2 | Binary search on data sorted descending |
Match mode -1 does the banded lookup that VLOOKUP with TRUE does, but it does not require sorted data. Search mode -1 is a favourite: =XLOOKUP(B5, Payments[Client], Payments[Date], , 0, -1) returns the date of a client's most recent payment, because it searches from the bottom of a list kept in date order. The empty fourth argument skips if_not_found.
XLOOKUP can also return several columns at once. If the return array is several columns wide, the result spills across. =XLOOKUP(E5, Packages[Code], Packages[[Name]:[Base fee]]) returns the package name, the included hours and the base fee in three adjacent cells. And because its result is a reference, XLOOKUP can be nested inside another XLOOKUP for a two-way lookup.
=XLOOKUP(G5, Rates[Lower limit], Rates[Rate], , -1)
=XLOOKUP(B5, Payments[Client], Payments[Date], "No payment", 0, -1)
=XLOOKUP("Harbour*", Venues[Name], Venues[Capacity], , 2)
=XLOOKUP(E5, Packages[Code], XLOOKUP(J2, Packages[#Headers], Packages))MATCH and INDEX
Before XLOOKUP, experienced users combined INDEX and MATCH, and the combination is still common and still tested. MATCH returns the position of a value in a single row or column. INDEX returns the value at a given position in a range. Together they do a lookup in any direction.
=MATCH("HC", Packages[Code], 0)
=INDEX(Packages[Base fee], 2)
=INDEX(Packages[Base fee], MATCH(E5, Packages[Code], 0))MATCH has a match_type argument: 0 for exact match, 1 (the default) for the largest value less than or equal to the lookup value in ascending data, and -1 for the smallest value greater than or equal to it in descending data. As with VLOOKUP, forgetting the 0 gives an approximate match. In Microsoft 365, XMATCH is the modern version with the same modes as XLOOKUP.
INDEX with a two-dimensional range takes both a row number and a column number, which makes the classic two-way lookup: one MATCH finds the row, another finds the column.
=INDEX(VenueRates, MATCH(C5, VenueNames, 0), MATCH(TEXT(D5,"mmm"), MonthNames, 0))Worked problem
Replacing a fragile VLOOKUP
The capacity formula is a VLOOKUP into a fixed range on the Venues sheet with column number 4 and FALSE for an exact match, and Venues is now a table with the Capacity column in a different position. Rewrite it two ways that survive column changes.
Show solution
- With XLOOKUP: =XLOOKUP(C5,Venues[Name],Venues[Capacity],"Not found").
- With INDEX and MATCH: =INDEX(Venues[Capacity],MATCH(C5,Venues[Name],0)).
- Test both by inserting a column into the Venues table.
- Test the missing case by typing a venue name that is not in the table.
Answer: Both formulas refer to the Capacity column by name, so inserting columns changes nothing. XLOOKUP shows "Not found" for a missing venue; INDEX/MATCH shows #N/A unless wrapped in IFNA.
Lookups with more than one condition
Some lookups need two keys. Maple & Pine's rental supplier charges a different price for each item and each season, and the price list has one row per item and season. To find the price of a chair in the high season, a lookup must match both columns. XLOOKUP handles this by searching an array built from a condition rather than a single column.
=XLOOKUP(1, (Rentals[Item]=B5)*(Rentals[Season]=C5), Rentals[Price], "No price")
=XLOOKUP(B5&"|"&C5, Rentals[Item]&"|"&Rentals[Season], Rentals[Price])
=INDEX(Rentals[Price], MATCH(1, (Rentals[Item]=B5)*(Rentals[Season]=C5), 0))The first form is the clearest. Each comparison produces an array of TRUE and FALSE values; multiplying them gives 1 only where both are TRUE, and XLOOKUP looks for that 1. The second form joins the item and season with a separator character on both sides, which works but is slower on large lists. The separator stops accidental matches, such as "Chair" plus "s" colliding with "Chairs" plus nothing. In older versions of Excel, the INDEX and MATCH form had to be entered with Ctrl+Shift+Enter as an array formula; in Microsoft 365 it works with Enter.
Returning every match
Every function in this lesson returns the first match. When Rosalind asked for "all of the Morrison family's bookings", a lookup was the wrong tool. FILTER, covered in the next lesson, returns every row that meets a condition: =FILTER(Bookings, Bookings[Client]="Morrison", "None"). A useful rule is that lookups answer "what is the value for this key?" and FILTER answers "which rows match this condition?".
Lookups across sheets and workbooks
Lookup ranges can be on other sheets or in other workbooks. Within a workbook, structured references and names make cross-sheet lookups readable: =XLOOKUP(E5, Packages[Code], Packages[Base fee]) works from any sheet, because table names are workbook-wide. Across workbooks, the arrays become external references, which put the file name in square brackets before the sheet name and the range, all inside single quotation marks when the names contain spaces. XLOOKUP and INDEX/MATCH update when the source is open and keep the last values when it is closed. Dagny avoids cross-workbook lookups for anything important; she imports the supplier list with Power Query instead, so the lookup always points at a table inside the budget workbook.
Worked problem
Commission bands without sorting
Coordinators earn a commission on each booking: 3 percent from 0, 4 percent from 5,000, 5 percent from 15,000 and 6 percent from 30,000. The Rates table lists lower limits and rates, but someone keeps re-sorting it by rate. Write a lookup that still returns the right band.
Show solution
- Use XLOOKUP with the fee as the lookup value and Rates[Lower limit] as the lookup array.
- Return Rates[Rate].
- Leave if_not_found empty and set match_mode to -1 for exact or next smaller.
- Test fees of 4,999, 5,000 and 31,250.
Answer: =XLOOKUP(G5,Rates[Lower limit],Rates[Rate],,-1) returns 3, 4 and 6 percent for the three test fees regardless of the table's order, which VLOOKUP with TRUE cannot guarantee.
Choosing among them
| Function | Strengths | Weaknesses |
|---|---|---|
| VLOOKUP / HLOOKUP | Familiar, works in every version | Right or down only; column number breaks; approximate by default |
| INDEX + MATCH | Any direction; two-way lookups; works in every version | Two functions to read; exact match must be specified |
| XLOOKUP | Any direction; exact by default; if_not_found; search last to first; spills multiple columns | Not available in Excel 2019 and earlier |
For new work in Microsoft 365, Dagny uses XLOOKUP. She keeps INDEX and MATCH for two-way grids and for files that a supplier opens in an older version of Excel. On the exam, use the function the task names; when it does not name one, any correct formula is accepted.
Troubleshooting lookups
Most lookup failures have the same few causes. #N/A means the value was not found: check for trailing spaces with TRIM, for numbers stored as text in one list and as numbers in the other, and for a missing exact-match argument. A wrong but plausible result usually means an approximate match on unsorted data. #REF! from VLOOKUP means the column number is larger than the table. #VALUE! from INDEX often means a row or column number of zero or less. Lesson 8 covers Evaluate Formula, which steps through a lookup to show exactly which part fails.
A quick diagnostic Dagny taught Teo uses a comparison in a spare cell. If a direct comparison such as =C5=A12 returns FALSE for two values that look identical, the lookup will fail too, and the cause is in the data rather than the formula. LEN on each value reveals hidden spaces, because the lengths differ, and ISNUMBER reveals a number stored as text. Fixing the data once, with TRIM or Text to Columns as described in lesson 2, repairs every lookup that depends on it, which is better than patching each formula with its own clean-up function.
Common mistakes
MistakeLeaving the last argument out of VLOOKUP or MATCH.
InsteadUse FALSE in VLOOKUP and 0 in MATCH for exact matches.
MistakeHard-coding a column number that changes when columns are inserted.
InsteadUse XLOOKUP or INDEX/MATCH with named columns.
MistakeUsing VLOOKUP to return a column to the left of the search column.
InsteadUse XLOOKUP or INDEX/MATCH.
MistakeWrapping every lookup in IFERROR.
InsteadUse if_not_found or IFNA so genuine formula errors still show.
MistakeComparing numbers stored as text with real numbers.
InsteadConvert one list so both use the same type.
Summary and key terms
- VLOOKUP and HLOOKUP search the first column or row and return by position; FALSE gives an exact match.
- XLOOKUP uses separate lookup and return arrays, defaults to exact match and has if_not_found, match modes and search modes.
- MATCH returns a position and INDEX returns a value at a position; together they look up in any direction.
- A two-way lookup uses INDEX with two MATCH functions, or nested XLOOKUP.
- Approximate matches are for banded lookups on sorted data, or XLOOKUP match mode -1.
- Lookup array
- The range searched for the lookup value.
- Return array
- The range from which XLOOKUP returns the matching value.
- Exact match
- A match that succeeds only when the value is found exactly.
- Approximate match
- A match that returns the closest smaller (or larger) value in sorted data.
- MATCH
- A function that returns the position of a value in a row or column.
- INDEX
- A function that returns the value at a given row and column position in a range.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Which XLOOKUP search mode finds the last matching entry?
Show answer
-1, search from last to first.
What does VLOOKUP do if the last argument is omitted?
Show answer
An approximate match.
Which function returns the position of a value in a list?
Show answer
MATCH (or XMATCH).
How does XLOOKUP handle a value that is not found?
Show answer
It returns the if_not_found argument, or #N/A if none is given.
Why does a VLOOKUP with a typed column number break when a column is inserted in the source?
Show answer
The column number stays the same while the data moves, so the formula returns the wrong column; XLOOKUP and INDEX/MATCH refer to the return column itself.
Which XLOOKUP match mode returns an exact match or the next larger item?
Show answer
1; -1 returns an exact match or the next smaller item.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.