Sub topic 8
Search modes: finding the last match
This chapter assumes you can write a basic XLOOKUP, with a lookup range, a result range, and a missing_value fallback. It also assumes you know what exact and approximate matching mean. Here you will learn the sixth argument, search_mode, which controls the direction and method XLOOKUP uses to scan the lookup range. The most common use is returning the last matching row instead of the first.
Why direction matters
A lookup returns one row, but a lookup column often contains the same key several times. A log, a ledger, or an order history typically has one row per event, so one customer or product appears many times.
Suppose A2:C7 holds a status log for products:
| Row | A (Product) | B (Date) | C (Status) |
|---|---|---|---|
| 2 | P-102 | 2026-01-03 | Ordered |
| 3 | P-205 | 2026-01-04 | Ordered |
| 4 | P-102 | 2026-01-09 | Shipped |
| 5 | P-310 | 2026-01-10 | Ordered |
| 6 | P-102 | 2026-01-15 | Delivered |
| 7 | P-205 | 2026-01-16 | Shipped |
P-102 appears three times. Asking for "the status of P-102" is ambiguous. If you want the current status, you need the bottom row, not the top one.
VLOOKUP always returns the first match, scanning from the top. XLOOKUP does the same by default:
=XLOOKUP("P-102", A2:A7, C2:C7)This returns Ordered, which is the oldest status and probably not what you want.
The search_mode argument
The full XLOOKUP signature is:
XLOOKUP(search_key, lookup_range, result_range, [missing_value], [match_mode], [search_mode])search_mode is the last argument, and it accepts four values:
| Value | Behavior | Requirement |
|---|---|---|
| 1 | Search from first to last. This is the default. | None |
| -1 | Search from last to first. | None |
| 2 | Binary search, lookup range sorted ascending. | Data must be sorted ascending |
| -2 | Binary search, lookup range sorted descending. | Data must be sorted descending |
This chapter focuses on -1. Modes 2 and -2 are covered later, with a warning about their risks.
Returning the last match with -1
To get the last row for a key, set search_mode to -1:
=XLOOKUP("P-102", A2:A7, C2:C7, "Not found", 0, -1)This returns Delivered. XLOOKUP starts at row 7 and works upward. It stops at the first matching cell it meets, which is row 6.
Because search_mode is the sixth argument, you must supply the arguments before it:
- The fourth argument is
missing_value. Here it is"Not found". - The fifth argument is
match_mode. Here it is0, which means exact match. This is also the default, so writing it only keeps the position correct.
Leaving optional arguments empty
You can skip an optional argument by leaving it blank between commas:
=XLOOKUP("P-102", A2:A7, C2:C7, , 0, -1)This works, but a missing key then returns #N/A. Supplying an explicit missing_value is usually clearer, and it saves you from wrapping the formula in IFNA.
A reliable way to get the latest record
Searching from the bottom returns the latest record only if newer rows sit below older rows. That holds for logs where you append a new row for each event, because the row order matches the time order.
If the sheet is not in time order, the bottom row is not the newest. In that case, either sort the data by date first or choose a different technique that compares dates. Do not assume the order. Check it.
This is a common source of silent errors. The formula never fails. It returns a plausible value that is wrong.
Combining search_mode with other arguments
search_mode works with every match_mode. The two arguments are independent. Match mode decides what counts as a match, and search mode decides which candidate is checked first.
Reverse search with a wildcard
Match mode 2 enables wildcards. Combined with -1, this returns the last row whose key fits a pattern:
=XLOOKUP("P-1*", A2:A7, C2:C7, "Not found", 2, -1)The pattern P-1* fits P-102 and P-310 is excluded, because it starts with P-3. Searching from the bottom, the first fitting row is row 6, so the result is Delivered.
Reverse search with approximate match
With match mode -1 or 1, XLOOKUP returns the nearest smaller or larger value if there is no exact match. The two arguments use the same numbers but mean different things. In match_mode, -1 is "exact or next smaller". In search_mode, -1 is "last to first". Mixing them up is a common mistake when you copy a formula.
=XLOOKUP(B2, $F$2:$F$6, $G$2:$G$6, "Below minimum", -1, -1)This asks for an exact or next smaller tier match, scanning from the bottom. For a tier table with unique thresholds, the direction does not change the answer. It matters only when the lookup column contains duplicates.
Reverse search with a different result range
The result range does not need to be next to the lookup range. You can look up in column A and return a value from column B, still searching from the bottom:
=XLOOKUP("P-102", A2:A7, B2:B7, "Not found", 0, -1)This returns the date of the last P-102 row, 2026-01-15. In a log, this is a convenient way to find when something last happened.
Binary search modes
Search modes 2 and -2 use a binary search. Instead of checking cells one by one, XLOOKUP repeatedly splits the range in half and keeps the half where the key must be. On very large ranges this can be faster than a linear scan.
- Mode
2requires the lookup range to be sorted ascending. - Mode
-2requires the lookup range to be sorted descending.
If the data is not sorted the way you declared, XLOOKUP does not report an error. It can return a wrong result or #N/A, and which one you get depends on the data. Binary search also gives no guarantee about which duplicate it returns. That is why mode -1, not 2, is the tool for "last match".
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Result is the first match, not the last | `search_mode` omitted or set to `1` | Add `-1` as the sixth argument |
| Formula returns the wrong column or a type error | Arguments shifted when a blank was skipped | Count commas, and supply `missing_value` and `match_mode` explicitly |
| Result is an old record | Rows are not in time order | Sort the data by date, or add a different lookup technique |
| Result changes unpredictably with mode `2` | Data not sorted ascending, or duplicates present | Use `1` or `-1`, or sort the data |
| `#N/A` with key that is clearly present | Hidden spaces or text-formatted numbers | Clean the key with TRIM or convert the type, as covered in the troubleshooting chapter |
Using reverse search across sheets
Search mode also works when the lookup and result ranges are on another sheet. Suppose a sheet named Log holds the status history, and your summary sheet lists product IDs in column A:
=XLOOKUP(A2, Log!A:A, Log!C:C, "Not found", 0, -1)Full-column ranges such as Log!A:A suit a log that keeps growing, because new rows are included automatically. Both ranges must be the same size. Searching a full column from the bottom starts at the last row of the sheet and scans upward past the empty cells. This works correctly, but a very large lookup repeated over many rows is slower than one on a bounded range.
Comparing with VLOOKUP
VLOOKUP has no search direction argument. Its only options for a duplicated key are the first match, or an approximate match that depends on sorted data. To get the last match with VLOOKUP you would have to reverse the data or build a helper column. XLOOKUP does it with one argument, which is one of the strongest reasons to prefer it for logs and histories.
Summary
search_modeis the sixth argument of XLOOKUP.1scans first to last and is the default.-1scans last to first and returns the last match.2and-2use binary search and need sorted data. They are rarely worth the risk.-1returns the bottom-most match, so it equals "latest" only when rows are in time order.- Search mode and match mode are separate arguments. Both use
-1, but with different meanings.
Exercise
A sheet has customer IDs in A2:A500, order totals in D2:D500, and new orders are always appended at the bottom. Write a formula that returns the most recent order total for the ID in F2, shows "No orders" when the ID is absent, and uses exact matching.
Show answer
=XLOOKUP(F2, A2:A500, D2:D500, "No orders", 0, -1) The fourth argument is the fallback, the fifth is exact match (0), and the sixth is -1 so the scan starts at the bottom and finds the newest row first.
Exercise
A colleague wrote =XLOOKUP("P-102", A2:A7, C2:C7, "Not found", -1) hoping to get the last match. It still returns "Ordered". What is wrong, and what is the corrected formula?
Show answer
The -1 sits in the fifth position, which is matchmode (exact or next smaller), not searchmode. Because P-102 exists, the exact match is found, and the scan still runs top to bottom. The corrected formula is =XLOOKUP("P-102", A2:A7, C2:C7, "Not found", 0, -1), which returns "Delivered".
Exercise
Why is search_mode 2 a poor choice for finding the last of several duplicate rows, even if the data is sorted ascending?
Show answer
Binary search does not guarantee which duplicate it returns, because it jumps through the range instead of scanning in order. Search mode -1 scans from the bottom, so it reliably returns the bottom-most match.