Sub topic 10
Searching first to last or last to first
This chapter assumes you can enter and copy formulas, use absolute references such as $E$2, and write a basic exact-match XLOOKUP. You need Excel for Microsoft 365, Excel 2021 or later, or Excel for the web.
Why search direction matters
A lookup range often contains the same value more than once. A customer may place several orders. A product price may be updated several times. A sensor may log many readings.
XLOOKUP returns only one result. When several entries match, the direction of the search decides which one you get. The search_mode argument controls that direction. It is the sixth and last argument of the function.
The search_mode argument
The full argument order is:
=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])The values of search_mode that matter in this chapter are:
| 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 data. Covered in a later chapter. |
| -2 | Binary search, descending data. Covered in a later chapter. |
By default, XLOOKUP starts at the top of the range and stops at the first match. Setting search_mode to -1 starts at the bottom and stops at the first match it finds going upward. That is the last match in the list.
Worked example: the most recent entry
Suppose A2:A100 holds customer names and C2:C100 holds order status. New orders are added at the bottom, so the newest order is the last row for each customer.
The default formula returns the oldest order:
=XLOOKUP(E2, A2:A100, C2:C100, "None")To get the most recent order, set search_mode to -1:
=XLOOKUP(E2, A2:A100, C2:C100, "None", 0, -1)The fifth argument, match_mode, is 0 because you want an exact match. You must write something in that position to reach the sixth argument. The value 0 is the default, so it changes nothing about how matching works.
Skipping optional arguments
You can leave an optional argument empty by keeping its comma. These two formulas behave the same when the lookup value is found:
=XLOOKUP(E2, A2:A100, C2:C100, , 0, -1)=XLOOKUP(E2, A2:A100, C2:C100, "None", 0, -1)If you skip if_not_found, a missing value returns #N/A. Supplying a message is usually friendlier for people reading the sheet.
Combining with match_mode
search_mode works with every match_mode. With wildcards, for example, -1 returns the last entry that fits the pattern:
=XLOOKUP("*"&E2&"*", A2:A100, C2:C100, "No match", 2, -1)With approximate matching, direction affects which row is returned only when the lookup column holds duplicate values. For unique values, the result is the same either way.
Common mistakes
- Putting
-1in thematch_modeposition. Then Excel looks for the next smaller item and the search still runs top to bottom. - Forgetting the commas for skipped arguments. The value lands in the wrong position.
- Assuming reverse search is slower or faster in a way that matters. For ordinary sheets, the difference is not noticeable.
- Using reverse search on data sorted by something other than time, then calling the result "latest."
Exercise
A sheet lists price changes in rows 2 to 200, oldest at the top. Column A holds product codes and column B holds prices. The code to look up is in D2. Write a formula that returns the current price, or "Unknown" if the code is missing.
Show answer
=XLOOKUP(D2, A2:A200, B2:B200, "Unknown", 0, -1). The -1 in the sixth position searches from the bottom, so the newest price for the code is returned.
Exercise
What does =XLOOKUP(E2, A2:A100, C2:C100, "None", -1) do, and why is it not a reverse search?
Show answer
The -1 sits in the fifth position, which is match_mode. It asks for an exact match or the next smaller item, and the search still runs from first to last. For a reverse search, the -1 must be the sixth argument: =XLOOKUP(E2, A2:A100, C2:C100, "None", 0, -1).