Find the Last Match in Excel With XLOOKUP
By ExcelReady TeamUpdated 7 min read
The short answer
To find the last match, set XLOOKUP's sixth argument, search_mode, to -1 so it searches from the bottom up: =XLOOKUP(F2,A2:A8,C2:C8,"Not found",0,-1). It returns the value from the last row where column A equals F2, such as the most recent price in a list sorted oldest first. XLOOKUP needs Excel 2021 or Microsoft 365.
Key takeaways
- XLOOKUP's 6th argument, search_mode, sets the direction: 1 is top to bottom (default), -1 is bottom to top.
- =XLOOKUP(F2,A2:A8,C2:C8,"Not found",0,-1) returns the last match.
- Don't put -1 in the 5th argument: that is match_mode, where -1 means "exact or next smaller".
- If the list isn't in date order, find the latest date with MAXIFS and look up both criteria.
- In older Excel, =LOOKUP(2,1/(A2:A8=F2),C2:C8) returns the last match.
On this page+
- Why lookups return the first match
- How to find the last match with XLOOKUP
- The mistake to avoid: match_mode vs search_mode
- How to find the position of the last match
- How to find the latest entry when the list isn't sorted
- How to get the last non-empty value in a column
- How to find the last match in older Excel
- Common mistakes when finding the last match
- Practise it
XLOOKUP finds the first match by default, but setting its last argument, search_mode, to -1 makes it search from the bottom up and return the last match instead. That's how you get the latest price, the most recent status or the last test score for a person from a log that grows downwards. This guide covers the formula, the argument most people mix up, and options for unsorted data and older Excel.
Why lookups return the first match#
VLOOKUP, MATCH and XLOOKUP all search from the top by default and stop at the first match. That's a problem for logs, where the newest row is at the bottom. Here is a supplier cost list, added to each week:
| A | B | C | |
|---|---|---|---|
| 1 | Product | Week | Unit cost |
| 2 | Paper | 1 | 4.20 |
| 3 | Toner | 1 | 38.00 |
| 4 | Paper | 2 | 4.35 |
| 5 | Pens | 3 | 6.10 |
| 6 | Toner | 4 | 36.50 |
| 7 | Paper | 5 | 4.50 |
| 8 | Pens | 6 | 5.90 |
With Paper in F2, =XLOOKUP(F2,A2:A8,C2:C8) returns 4.20, the week 1 cost. You want 4.50, the latest one, from week 5. A plain VLOOKUP has the same problem and no way to change direction.
How to find the last match with XLOOKUP#
Add the 4th, 5th and 6th arguments, ending with -1:
=XLOOKUP(F2,A2:A8,C2:C8,"Not found",0,-1)The result is 4.5. Here is what each argument does:
| Argument | Value | Meaning |
|---|---|---|
| lookup_value | F2 | Find "Paper" |
| lookup_array | A2:A8 | in the Product column |
| return_array | C2:C8 | and return the Unit cost |
| if_not_found | "Not found" | Text to show if there's no match |
| match_mode | 0 | Exact match (the default) |
| search_mode | -1 | Search from the last row to the first |
XLOOKUP starts at row 8, moves up, and stops at the first "Paper" it meets: row 7. You can leave the 4th and 5th arguments empty with commas, =XLOOKUP(F2,A2:A8,C2:C8,,,-1), but writing them out makes the formula easier to check. XLOOKUP needs Excel 2021, Microsoft 365 or Excel for the web; the XLOOKUP guide and the XLOOKUP function reference cover the basics.
The mistake to avoid: match_mode vs search_mode#
The most common error is putting -1 in the 5th argument instead of the 6th.
=XLOOKUP(F2,A2:A8,C2:C8,,-1)That -1 is match_mode, which means "exact match, or the next smaller value". For text that exists, it still returns the first match, 4.20, so the formula looks fine but answers the wrong question. Count the commas: search_mode is the sixth argument.
| Argument | 1 | -1 | 2 |
|---|---|---|---|
| 5th: match_mode | Exact or next larger | Exact or next smaller | Wildcard |
| 6th: search_mode | First to last (default) | Last to first | Binary search (sorted data) |
Match modes 2 and wildcards are covered in XLOOKUP wildcard match. You can combine them with search_mode: =XLOOKUP("P*",A2:A8,A2:A8,,2,-1) returns the last product starting with P, which is Pens.
How to find the position of the last match#
To get the row number instead of the value, use XMATCH with the same -1:
=XMATCH(F2,A2:A8,0,-1)The result is 6: the last Paper is the 6th row of A2:A8. Use it with INDEX to return from any column: =INDEX(C2:C8,XMATCH(F2,A2:A8,0,-1)) also returns 4.5. MATCH has no search direction, so it can only give you the first position; see the MATCH function for what it can do.
How to find the latest entry when the list isn't sorted#
search_mode -1 returns the last row, which is the latest only if the list is in date order. If rows can arrive out of order, find the latest date first, then look up the row with both the product and that date.
Step 1: Find the latest week for the product#
=MAXIFS(B2:B8,A2:A8,F2)This returns 5, the highest week number for Paper. MAXIFS needs Excel 2019 or later.
Step 2: Look up the row with both criteria#
=XLOOKUP(1,(A2:A8=F2)*(B2:B8=MAXIFS(B2:B8,A2:A8,F2)),C2:C8)This returns 4.5, wherever the week 5 row sits in the list. Multiplying the two tests is the standard way to look up on two conditions; see lookup with multiple criteria for how it works.
How to get the last non-empty value in a column#
The same idea finds the last filled cell, such as the latest reading in a column that grows each day:
=XLOOKUP(TRUE,C2:C100<>"",C2:C100,,0,-1)C2:C100<>"" is TRUE for every filled cell. Searching from the bottom, XLOOKUP returns the value of the last one: here, 5.9. The range can extend past your data, so new rows are picked up automatically.
How to find the last match in older Excel#
Without XLOOKUP, the classic formula uses LOOKUP:
=LOOKUP(2,1/(A2:A8=F2),C2:C8)1/(A2:A8=F2) gives 1 for matching rows and #DIV/0! for the rest. LOOKUP searches for 2, can't find it, ignores the errors, and settles on the last 1 in the list. It returns the matching value from C2:C8: 4.5. It works in all versions without Ctrl+Shift+Enter, but it's hard to read, so add a note next to it.
Two other choices: sort the data newest first (Data > Sort, Largest to Smallest on the date column) so a normal VLOOKUP finds the latest row, or use a pivot table with the MAX of the date.
Common mistakes when finding the last match#
- -1 in the wrong slot. As above, the 5th argument is match_mode; search_mode is the 6th.
- Assuming "last" means "latest". search_mode -1 returns the bottom-most row. If the data isn't in date order, use the MAXIFS method.
- Ranges of different sizes, such as A2:A8 and C2:C9, which return #VALUE!.
- Extra spaces or text-numbers in the lookup column, which make XLOOKUP skip the row you expect. Clean the data with TRIM, or convert text to numbers.
- Forgetting if_not_found. Without it, a product with no rows shows #N/A, which can break totals that use the result.
Practise it#
Try the live exercise on this page to return the latest Paper cost. Then practise XLOOKUP wildcards and match modes in a live spreadsheet, where you'll use XLOOKUP's optional arguments, including the match modes this guide warns you not to confuse with search_mode.
Try it yourself
This is a live spreadsheet. Type the formula in the green cell and press Enter.
The cost list is in date order, oldest first. In G2, return the latest unit cost for the product in F2. Use XLOOKUP with "Not found", match_mode 0 and search_mode -1.
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Unit cost | Latest cost | ||||||
| 2 | Paper | |||||||
| 3 | ||||||||
| 4 | ||||||||
| 5 | ||||||||
| 6 | ||||||||
| 7 | ||||||||
| 8 | ||||||||
| 9 | ||||||||
| 10 | ||||||||
| 11 |
Tip: Write a normal XLOOKUP, then add "Not found", 0 for exact match, and -1 to search from the bottom.
Practise this in a lesson
Frequently asked questions
How do I get XLOOKUP to return the last match?
Set the sixth argument, search_mode, to -1: =XLOOKUP(F2,A2:A8,C2:C8,"Not found",0,-1). XLOOKUP then searches from the last row up and returns the first match it meets, which is the last one in the list.
Can VLOOKUP find the last match?
No. VLOOKUP always searches from the top. Use XLOOKUP with search_mode -1, sort the data newest first, or use the LOOKUP(2,1/...) formula in older versions.
What is the difference between match_mode and search_mode in XLOOKUP?
match_mode (5th argument) controls how values match: exact, next smaller, next larger or wildcard. search_mode (6th argument) controls the direction: first to last or last to first.
How do I find the latest entry by date, not by position?
Find the latest date with MAXIFS, then look up the row that has both the item and that date: =XLOOKUP(1,(A2:A8=F2)*(B2:B8=MAXIFS(B2:B8,A2:A8,F2)),C2:C8).
How do I get the last non-empty value in a column?
Use =XLOOKUP(TRUE,C2:C100<>"",C2:C100,,0,-1). It looks for the last cell that is not empty and returns its value.