Sub topic 9
XLOOKUP options for missing keys and match modes
This chapter covers the optional arguments of XLOOKUP: what to return when a key is missing, how the match mode changes what counts as a match, and how the search mode controls which row wins. It assumes you can write a basic exact-match XLOOKUP, use absolute and relative references, and read a flat assumptions table with one key per row.
The full signature
XLOOKUP takes six arguments. Only the first three are required.
=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])| Argument | Purpose | Default |
|---|---|---|
| lookup_value | The key you are searching for | none |
| lookup_array | The range that holds the keys | none |
| return_array | The range that holds the values to return | none |
| if_not_found | What to return when no match exists | #N/A error |
| match_mode | How a key is compared with the lookup array | 0 (exact match) |
| search_mode | The direction and method of the search | 1 (first to last) |
The optional arguments are positional. To set matchmode without setting ifnot_found, leave the fourth slot empty but keep its comma.
Handling a missing key with if_not_found
By default XLOOKUP returns #N/A when the key is absent. In a financial model that error spreads into every formula that depends on the cell. Sometimes that is what you want, because a loud failure is hard to miss. The fourth argument lets you choose a different result.
=XLOOKUP($A5, Assumptions!$B$3:$B$12, Assumptions!$C$3:$C$12, "Missing assumption")If the key in $A5 is not in the list, the cell shows the text Missing assumption instead of #N/A.
Choosing a fallback value
The fallback you choose decides how the failure behaves downstream.
- Text, such as "Missing assumption". Any arithmetic that uses the cell returns #VALUE!. The failure is still visible, but the message is easier to read at the source cell.
- Zero. Downstream formulas keep working, so a missing growth rate or tax rate quietly becomes 0%. The total looks plausible and is wrong.
- An empty string (""). This behaves like text in arithmetic and hides the problem in the source cell. Avoid it for numeric inputs.
<br>
if_not_found is not a general error trap
The fourth argument handles only the case where the key is not found. It does not catch other errors. If the return array contains an error, XLOOKUP returns that error. If the arrays have different sizes, you get #VALUE!. Wrapping the whole formula in IFERROR would hide these failures along with the missing key, so prefer ifnotfound when the missing key is the only case you expect.
Match modes
The fifth argument, match_mode, controls how the lookup value is compared with the keys.
| match_mode | Behavior |
|---|---|
| 0 | Exact match. Returns #N/A (or the fallback) if no key is equal. This is the default. |
| -1 | Exact match, or else the next smaller item. |
| 1 | Exact match, or else the next larger item. |
| 2 | Wildcard match using *, ?, and ~. |
Exact match (0)
Mode 0 is the right choice for keys such as account codes, scenario names, and line-item labels. Text comparison ignores case, so "Revenue" and "REVENUE" match. It does not ignore trailing spaces, so "Revenue " with a trailing space is a different key. Stray spaces are a common cause of unexpected not-found results.
Next smaller item (-1)
Mode -1 is the basis for tier tables. It looks for an exact match and, failing that, returns the item with the largest key that is still below the lookup value.
=XLOOKUP(B5, Tiers!$B$3:$B$7, Tiers!$C$3:$C$7, , -1)The double comma skips ifnotfound, so the formula keeps the default error. Suppose the thresholds in B3:B7 are 0, 1000, 5000, 10000, and 25000, with a rate for each in column C. A value of 7200 has no exact match. With mode -1 it returns the rate for 5000, the largest threshold that does not exceed 7200. This is the behavior you need for volume discounts, commission bands, and tax brackets.
If the value is smaller than every threshold, the formula returns #N/A. Make sure the table starts at 0, or at a value lower than any input you expect.
Next larger item (1)
Mode 1 returns the smallest key that is at least as large as the lookup value. Use it when a band is defined by its upper limit, such as "up to 1,000 units costs X, up to 5,000 units costs Y". Check which way your table is defined before you pick -1 or 1. Choosing the wrong one shifts every value by one band without producing an error.
Wildcard match (2)
Mode 2 treats * as any run of characters, ? as any single character, and ~ as an escape for a literal wildcard character.
=XLOOKUP("Opex*", Data!$C$2:$C$5000, Data!$F$2:$F$5000, "None", 2)This returns the amount from the first row whose category starts with Opex. XLOOKUP returns one value, so it does not total all the matching rows. Use SUMIFS with a wildcard criterion for that task.
<br>
Search modes
The sixth argument, search_mode, controls the order in which XLOOKUP examines the lookup array.
| search_mode | Behavior |
|---|---|
| 1 | Search from the first item to the last. This is the default. |
| -1 | Search from the last item to the first. |
| 2 | Binary search, ascending order. The array must be sorted. |
| -2 | Binary search, descending order. The array must be sorted. |
Which duplicate wins
When a key appears more than once, mode 1 returns the first match and mode -1 returns the last. This is useful when a table is appended over time and the latest entry should apply.
=XLOOKUP($A5, Rates!$B$3:$B$500, Rates!$C$3:$C$500, "No rate", 0, -1)Here the most recent rate for the key in $A5 is returned, assuming newer rows are added at the bottom. This relies on a row order convention, so document it. A better design gives each rate an effective date and looks up on that, but the reverse search is a reasonable fix for an append-only list.
Binary search modes
Modes 2 and -2 are faster on very large sorted lists, because they do not scan every row. If the data is not actually sorted, the result can be wrong and no error warns you. For the sizes in most financial models, the speed difference is not noticeable. Prefer modes 1 and -1 unless you have measured a calculation-time problem.
Combining the options
The options work together. This formula finds a commission rate for a sales value, falls back to a clearly labeled text if the sales value is below the first tier, and searches the tier table for an exact match or the next smaller item.
=XLOOKUP(B5, Tiers!$B$3:$B$7, Tiers!$C$3:$C$7, "Below first tier", -1)Check the argument positions. "Below first tier" fills ifnotfound and -1 fills match_mode, so no empty slot is needed.
Common errors
- Wrong argument position. Writing
XLOOKUP(B5, keys, values, -1)puts -1 in the ifnotfound slot, so the formula returns -1 on a miss and still does an exact match. Count the commas. - Mismatched array sizes. The lookup array and return array must have the same number of rows (or columns). Otherwise the result is #VALUE!.
- Text numbers. A key stored as text, such as "1001", does not match the number 1001. Convert both sides to the same type.
- Unsorted tier tables. Modes -1 and 1 work on unsorted data with the default search mode, but sort the thresholds anyway so the table is readable and safe to switch to binary search.
- Hidden fallback. A fallback that looks like valid data, such as 0, defeats the purpose of the missing-key check.
Exercise
A tier table has thresholds 0, 500, 2000, and 10000 in Tiers!B3:B6 and discount rates 0%, 2%, 5%, and 8% in Tiers!C3:C6. Write an XLOOKUP for the discount on the order value in B5 that returns the text "Check order value" if no tier applies. What does it return for an order of 2000, and for an order of 1999?
Show answer
=XLOOKUP(B5, Tiers!$B$3:$B$6, Tiers!$C$3:$C$6, "Check order value", -1). An order of 2000 matches the threshold 2000 exactly and returns 5%. An order of 1999 has no exact match, so mode -1 returns the next smaller threshold, 500, and the rate is 2%. A negative order value is below every threshold and returns "Check order value".
Exercise
A rates list in Rates!B3:B500 (keys) and Rates!C3:C500 (rates) is appended every quarter, so a key can appear several times and the newest row is at the bottom. Which search_mode returns the newest rate for the key in $A5, and why is an effective date column a safer design?
Show answer
Use search_mode -1, which searches from the last row to the first, so the first match found is the newest row: =XLOOKUP($A5, Rates!$B$3:$B$500, Rates!$C$3:$C$500, "No rate", 0, -1). It depends on rows staying in chronological order, and sorting or inserting a row in the middle would silently change the result. An effective date column makes the rule explicit and independent of row order.