Sub topic 2
XLOOKUP: a lookup range and a result range
This chapter assumes you can enter formulas in Google Sheets, read cell ranges such as A2:A100, and anchor references with $. It builds on the VLOOKUP basics from earlier chapters.
Separate lookup and result ranges
XLOOKUP takes a search key, a lookup range, and a result range. The lookup range is searched for the key. The result range supplies the value from the matching position. The two ranges must be the same size, but they can be in any column order, so the result can sit to the left of the key.
=XLOOKUP("P-102", A2:A100, C2:C100)This formula searches A2:A100 for "P-102" and returns the value in C2:C100 from the same row.
Handling keys that are not found
When the key is missing, XLOOKUP returns #N/A. The fourth argument replaces that error with a value you choose.
=XLOOKUP(E2, A2:A100, C2:C100, "Not found")Match modes: exact, nearest, and wildcard
The fifth argument, match_mode, controls how a key is compared with the lookup range.
| match_mode | Behavior |
|---|---|
| 0 (default) | Exact match only |
| -1 | Exact match, or the next smaller value |
| 1 | Exact match, or the next larger value |
| 2 | Wildcard match, where * matches any text and ? matches one character |
Use -1 to find the price tier for an amount. The lookup range does not need to be sorted.
=XLOOKUP(B2, $F$2:$F$6, $G$2:$G$6, "Below minimum", -1)Use 2 to find the first item whose name starts with "Wid".
=XLOOKUP("Wid*", B2:B100, A2:A100, "Not found", 2)Search modes: finding the last match
The sixth argument, search_mode, sets the search direction. The default, 1, searches from first to last and returns the first match. Set it to -1 to search from last to first and return the last match. This is useful when a key appears more than once, such as a repeated order ID with the newest entry at the bottom.
=XLOOKUP(E2, A2:A100, C2:C100, "Not found", 0, -1)Exercise
In the formula =XLOOKUP(E2, A2:A100, C2:C100, "Not found", 0, -1), which argument makes the function return the last match, and what value does it use?
Show answer
The sixth argument, search_mode, set to -1, searches from the bottom up so the last matching row is returned.