Sub topic 2
XLOOKUP syntax: the six arguments
This chapter assumes you can enter and copy formulas, that you understand cell ranges and absolute references such as $E$2, and that you use Excel for Microsoft 365, Excel 2021 or later, or Excel for the web. Earlier versions do not have XLOOKUP.
The full signature
XLOOKUP searches one range for a value and returns the item in the same position from another range. Its complete form has six arguments, three required and three optional:
=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])Square brackets mark an optional argument. If you leave out the optional arguments, XLOOKUP does an exact match, searches from the first item to the last, and returns #N/A when nothing matches.
| Position | Argument | Required | Default | Purpose |
|---|---|---|---|---|
| 1 | `lookup_value` | Yes | None | The value to find |
| 2 | `lookup_array` | Yes | None | The range to search |
| 3 | `return_array` | Yes | None | The range to return a value from |
| 4 | `if_not_found` | No | `#N/A` | What to return when there is no match |
| 5 | `match_mode` | No | 0 (exact) | How strictly values must match |
| 6 | `search_mode` | No | 1 (first to last) | Which direction to search, or whether to use binary search |
A worked example
The examples in this chapter use a small product table in A1:C6.
| Row | A | B | C |
|---|---|---|---|
| 1 | ID | Product | Price |
| 2 | 101 | Keyboard | 49 |
| 3 | 102 | Mouse | 19 |
| 4 | 103 | Monitor | 189 |
| 5 | 104 | Webcam | 59 |
| 6 | 105 | Headset | 79 |
Cell F2 holds the ID you want to look up, for example 103. To get the price:
=XLOOKUP(F2, A2:A6, C2:C6)XLOOKUP finds 103 at position 3 in A2:A6. It then returns the item at position 3 in C2:C6, which is 189. To get the product name instead, change only the third argument:
=XLOOKUP(F2, A2:A6, B2:B6)The result is Monitor. The lookup column stays the same and the return column changes. This is a real difference from VLOOKUP, which counts columns with a number and breaks when someone inserts a column. XLOOKUP points at the return range directly, so inserting a column between A and C adjusts both references automatically.
The three required arguments
lookup_value
This is what you are looking for. It is usually a cell reference such as F2, but it can also be a typed constant or a calculated value:
=XLOOKUP(103, A2:A6, B2:B6)
=XLOOKUP("Monitor", B2:B6, C2:C6)
=XLOOKUP(F2 + 1, A2:A6, B2:B6)Text is written in double quotes. Numbers are not. The lookup value and the lookup array must hold the same data type, and chapter 5 returns to this in detail.
lookup_array
This is the single row or single column that XLOOKUP searches. It must be one-dimensional. If you pass a multi-column block such as A2:B6 as the lookup array, XLOOKUP returns #VALUE!.
The lookup array does not have to be the leftmost column of your table. You can search column C and return column A. VLOOKUP cannot do this, because it only looks right of the search column.
return_array
This is the range that supplies the answer. The rule is that it must line up with the lookup array along the search direction:
- If the lookup array is a column, the return array must have the same number of rows.
- If the lookup array is a row, the return array must have the same number of columns.
The return array may be wider (or taller) than one cell in the other direction. This is how you return several columns at once, which a later chapter covers. If the sizes do not agree, the result is #VALUE!.
=XLOOKUP(F2, A2:A6, C2:C5)This formula fails with #VALUE! because A2:A6 has five rows and C2:C5 has four.
The three optional arguments
if_not_found
When no match exists, XLOOKUP normally returns #N/A. The fourth argument replaces that with a value of your choice:
=XLOOKUP(F2, A2:A6, C2:C6, "Not found")You can pass text, a number, a cell reference, or another formula. Two details matter:
if_not_foundonly replaces the "no match" result. It does not hide other errors. A#VALUE!caused by mismatched array sizes still appears.- If you want the default
#N/Abut need to set a later argument, leave the position empty by keeping the commas. The formula=XLOOKUP(F2, A2:A6, C2:C6, , 1)setsmatch_modeto 1 and still returns#N/Afor a missing value.
An empty text string "" is different from an empty position. Writing "" returns a blank-looking cell instead of #N/A, which can hide a problem if later formulas depend on the result.
match_mode
The fifth argument controls how a cell in the lookup array counts as a match.
| Value | Behavior |
|---|---|
| 0 | Exact match. This is the default. |
| -1 | Exact match, or the next smaller item if there is no exact match |
| 1 | Exact match, or the next larger item if there is no exact match |
| 2 | Wildcard match, where `*`, `?` and `~` have special meaning |
The default is exact, which is the opposite of VLOOKUP and MATCH, where the default can be approximate. A forgotten argument in XLOOKUP gives you the safer result. Later chapters cover modes -1, 1 and 2 in depth. Here is a quick preview for a tiered grade lookup:
=XLOOKUP(B2, $E$2:$E$6, $F$2:$F$6, "Below minimum", -1)The lookup array in match_mode -1 does not need to be sorted. This is another difference from the approximate modes of older functions.
search_mode
The sixth argument sets the order in which XLOOKUP examines the lookup array.
| Value | 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 required |
| -2 | Binary search, descending order required |
The direction matters when the lookup array contains duplicates. XLOOKUP always returns the first match it meets, so with the default direction you get the topmost entry, and with -1 you get the bottommost one:
=XLOOKUP(E2, A2:A100, C2:C100, "None", 0, -1)The binary search modes are faster on very large lists, but they give wrong answers on data that is not sorted in the stated order. XLOOKUP does not check the order for you.
Skipping to a later argument
Arguments are positional. To set search_mode, you must fill or skip every argument before it. You can skip with an empty position, but the comma still has to be present:
=XLOOKUP(F2, A2:A6, C2:C6, , , -1)This searches from the last item and keeps the defaults for if_not_found and match_mode. When you are unsure, writing the default value explicitly is clearer to the next reader of your workbook:
=XLOOKUP(F2, A2:A6, C2:C6, "Not found", 0, -1)Locking ranges when you copy the formula
Formulas are usually filled down a column, so the ranges must not shift. Use absolute references for the arrays and leave the lookup value relative:
=XLOOKUP(F2, $A$2:$A$6, $C$2:$C$6, "Not found")When this formula is copied from G2 to G3, F2 becomes F3, while the arrays stay fixed. If you forget the dollar signs, the arrays slide down one row per copy and eventually lookups start failing for rows that should match. Whole-column references such as $A:$A avoid this problem and also pick up new rows. They work in XLOOKUP without the heavy slowdown some older functions had, but they can still be slower than exact ranges on very large workbooks.
Common mistakes at the syntax level
- Multi-column lookup array.
A2:B6as the second argument returns#VALUE!. Search one column only. - Unequal array sizes. The lookup and return arrays must have the same length along the search direction.
- Text without quotes.
=XLOOKUP(Monitor, B2:B6, C2:C6)makes Excel look for a name called Monitor and returns#NAME?. Write"Monitor". - Wrong argument position. Putting the match mode in the fourth slot makes it the
if_not_foundvalue, so XLOOKUP quietly returns that number when nothing matches. - Assuming a number is the same as text. A lookup value of
103does not match a cell holding the text"103". Both look identical on screen, but XLOOKUP treats them as different values.
Summary of the working model
XLOOKUP always does three things in order. It searches lookup_array for lookup_value, using the comparison rule from match_mode and the direction from search_mode. It records the position of the match. It then returns the item at that same position in return_array, or the if_not_found result when there is no match. Every other feature in the later chapters is a variation on those three steps.
Exercise
A formula is written as =XLOOKUP(F2, A2:B6, C2:C6) and returns #VALUE!. What is wrong, and what is the corrected formula?
Show answer
The lookup array A2:B6 covers two columns, but it must be a single row or column. Use =XLOOKUP(F2, A2:A6, C2:C6), or point the lookup array at whichever single column holds the values you are searching.
Exercise
You want the price from C2:C6 for the ID in F2, but a missing ID should show the text "Unknown ID", and you want the last match if the ID appears more than once. Write the formula, and say which value you must leave at its default.
Show answer
=XLOOKUP(F2, $A$2:$A$6, $C$2:$C$6, "Unknown ID", 0, -1). The matchmode stays at its default of 0 (exact match). It can be written as 0 or left empty with an extra comma, as in =XLOOKUP(F2, $A$2:$A$6, $C$2:$C$6, "Unknown ID", , -1). The searchmode of -1 searches from the last item to the first, so the last duplicate is returned.