Sub topic 5
XLOOKUP: a lookup range and a result range
XLOOKUP is Google Sheets' newer lookup function. It does the same job as VLOOKUP: find a key in one column and return a value from the same row. The difference is that you give it two separate ranges, one to search and one to read the answer from. This chapter covers the syntax, how it compares to VLOOKUP, and the errors you are most likely to hit.
Prerequisites
This chapter assumes you can:
- Enter formulas in a cell.
- Read range references such as
A2:C100. - Use absolute references such as
$A$2:$A$100. - Know how VLOOKUP works, including its column index and its exact-match argument.
The syntax
=XLOOKUP(search_key, lookup_range, result_range, [missing_value], [match_mode], [search_mode])Only the first three arguments are required.
| Argument | Meaning |
|---|---|
| `search_key` | The value you want to find. |
| `lookup_range` | The single row or column that is searched. |
| `result_range` | The range the answer is read from. It must line up with the lookup range. |
| `missing_value` | Optional. What to return if the key is not found. |
| `match_mode` | Optional. How keys are compared. The default is 0, an exact match. |
| `search_mode` | Optional. Which direction to search. The default is 1, first to last. |
This chapter focuses on the first three arguments. Later chapters cover the optional ones in detail.
A first example
Suppose a product list sits in A2:C100. Column A holds product IDs, column B holds names, and column C holds prices. To get the price of product P-102:
=XLOOKUP("P-102", A2:A100, C2:C100)Sheets searches A2:A100 for P-102. If the key is in the fifth cell of that range, Sheets returns the fifth cell of C2:C100. The match is by position inside the two ranges.
Compare it with the VLOOKUP version:
=VLOOKUP("P-102", A2:C100, 3, FALSE)The two formulas return the same result, but they work differently:
- XLOOKUP matches exactly by default. VLOOKUP defaults to approximate match, so you must remember to add
FALSE. - XLOOKUP names the result column directly. VLOOKUP counts columns with the number
3. - XLOOKUP reads the lookup and result ranges separately. VLOOKUP treats the whole table as one block.
Why separate ranges help
No column counting
With VLOOKUP, the 3 in the formula is a count from the first column of the table. Suppose someone inserts a column inside A:C. The table range expands, but the 3 may now point to the wrong column, and the formula can quietly return the wrong data.
An XLOOKUP result range refers to the real column. Sheets adjusts the reference when columns are inserted or deleted. If a column is deleted, the formula shows a #REF! error rather than a wrong answer.
Looking to the left
VLOOKUP can only return values from columns to the right of the lookup column. XLOOKUP has no such rule. To search column C and return the matching value from column A:
=XLOOKUP(E2, C2:C100, A2:A100, "Not found")The lookup range and result range can be in any order, and they can even sit on different sheets.
Smaller ranges
XLOOKUP only touches the two ranges you give it. It does not need to carry the unused columns between the lookup column and the result column.
The ranges must match in size
The result range must have the same number of rows as the lookup range when you search down a column. XLOOKUP pairs cells by position, so a mismatch either gives an error or points to the wrong row.
Here is a mismatched pair:
=XLOOKUP("P-102", A2:A100, C3:C101)Both ranges have 99 rows, so there is no error. But the result range starts one row lower than the lookup range, so every answer is shifted by one row. Sheets cannot warn you about this, because the formula is valid. Check that the first row of both ranges is the same row number.
If the ranges have different sizes, Sheets returns an error:
=XLOOKUP("P-102", A2:A100, C2:C50)This formula fails because 99 lookup rows cannot map onto 49 result rows.
A good habit is to write both ranges with the same start and end rows, and to lock them with $ if you copy the formula down:
=XLOOKUP(E2, $A$2:$A$100, $C$2:$C$100)Without the $, copying the formula down one row would shift both ranges down with it, and later lookups would search the wrong rows.
Exact match by default
When you leave out match_mode, XLOOKUP uses exact matching. Three points follow from this:
P-102does not matchp-102with a trailing space. Case is ignored, but spaces are not.- The number
102does not match the text"102". Numbers stored as text are a frequent cause of failures. - If there is no exact match, the result is
#N/A.
The #N/A error means "not available": Sheets could not find the key. Chapter content on handling it follows later, but there is a short version worth knowing now. The fourth argument lets XLOOKUP return your own text in place of the error:
=XLOOKUP(E2, A2:A100, C2:C100, "Not found")If E2 is in the list, you get the price. If not, you get Not found.
What happens with duplicate keys
If the key appears more than once in the lookup range, XLOOKUP returns the result for the first match, reading from the top of the range down. Later matches are ignored. A product table with two rows for P-102 returns only the price in the upper row.
You can change the direction with the sixth argument, which a later chapter explains. For now, remember that XLOOKUP never warns you about duplicates. If your keys should be unique, check this yourself, for example with a COUNTIF over the key column.
=COUNTIF($A$2:$A$100, E2)A result greater than 1 means the key is duplicated.
Worked example: an order sheet
You have an Orders sheet with product IDs in column A, starting at A2. A separate range holds the product list in F2:H50: IDs in F, names in G, prices in H. You want the product name next to each order.
In B2, enter:
=XLOOKUP(A2, $F$2:$F$50, $G$2:$G$50, "Unknown product")Then in C2, get the price from the same table:
=XLOOKUP(A2, $F$2:$F$50, $H$2:$H$50, "Unknown product")Copy both formulas down the column. Each row searches the same fixed table, because the $ signs lock the ranges. The search key A2 has no $ and moves down with each row.
Points to notice:
- The lookup range is the same in both formulas. Only the result range changes.
- You did not count columns. If you later add a column between G and H, the formulas follow the data.
- Rows with an ID that is not in the list show
Unknown product, which is easier to spot than#N/Ascattered through a report.
XLOOKUP and whole-column references
You can use full-column references such as A:A and C:C. They are convenient because new rows are included automatically:
=XLOOKUP(E2, A:A, C:C, "Not found")The trade-off is that the header row is searched too. If a header is exactly the same as your key, the formula matches it. Full-column ranges also search many empty cells, which can slow a large sheet that has thousands of these formulas. For fixed tables, bounded ranges with $ are safer. For tables that grow, full columns are usually acceptable.
Common mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| Result range starts on a different row than the lookup range | Every answer comes from the wrong row | Give both ranges the same start and end rows |
| Ranges of different sizes | Error | Make the row counts equal |
| Forgot `$` when copying down | Lookups work at first, then fail lower down | Lock both ranges with `$` |
| Key has trailing spaces | `#N/A` | Clean the key with `TRIM` |
| Key is text but the table holds numbers, or the reverse | `#N/A` | Convert one side so both have the same type |
| Passing a whole table as the result range, then expecting one value | Several cells fill with values | Use a single-column result range |
The last row of this table matters. If the result range has several columns, XLOOKUP returns the whole matching row and spills it into the cells to the right. That behavior is useful when you want it, and a later chapter covers it. When you want one value, point the result range at one column.
Quick comparison with VLOOKUP
| Task | VLOOKUP | XLOOKUP |
|---|---|---|
| Return a value from the right | Yes | Yes |
| Return a value from the left | No | Yes |
| Default match | Approximate | Exact |
| Result chosen by | Column number | Result range |
| Built-in "not found" text | No, use `IFNA` | Yes, fourth argument |
Exercise
A product table has IDs in A2:A100, names in B2:B100, and prices in C2:C100. The ID to find is in E2. Write an XLOOKUP formula that returns the name for that ID, or the text No such ID if it is missing. Then explain what goes wrong with =XLOOKUP(E2, A2:A100, B3:B101).
Show answer
=XLOOKUP(E2, A2:A100, B2:B100, "No such ID"). In the faulty formula, both ranges have 99 rows, so there is no error, but the result range starts one row below the lookup range. Each match returns the name from the row below the one that holds the ID, so every answer is wrong and Sheets gives no warning.
Exercise
You copy =XLOOKUP(A2, F2:F50, G2:G50) from B2 down to B3. What formula does B3 contain, and why can this cause #N/A results? How do you fix it?
Show answer
B3 contains =XLOOKUP(A3, F3:F51, G3:G51). The relative ranges move down one row, so the lookup skips the first table row and runs past the end of the data. Keys in the first table row now return #N/A. Fix it by locking the ranges: =XLOOKUP(A2, $F$2:$F$50, $G$2:$G$50).