Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

XLOOKUP search_mode -1: Search Last to First

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To make XLOOKUP return the last matching row instead of the first, set the sixth argument, search_mode, to -1. The full pattern is =XLOOKUP(lookup_value, lookup_array, return_array, if_not_found, match_mode, -1). With -1, Excel walks the lookup array from the bottom up and stops at the first match it meets, which is the last occurrence in the data. This is the standard fix for duplicate IDs, repeated customer names, or any list where the newest entry sits at the bottom and you want that one, not the oldest. Nothing needs to be installed or enabled first: search_mode -1 is part of XLOOKUP itself, available in Excel for Microsoft 365, Excel 2021 and later, and Excel for the web. If XLOOKUP returns #NAME?, your Excel version predates it and you will need the INDEX/MATCH alternative described further down.

What You'll Need

  • A version of Excel that includes XLOOKUP: Microsoft 365, Excel 2021 or newer, or Excel for the web. Excel 2019 and earlier do not have it.
  • A lookup column and a return column on the same sheet or in the same workbook.
  • Your data sorted so that the entry you want is physically lower in the column than the older duplicates. search_mode -1 is positional, not date-aware.
  • Optional but useful: a small helper cell holding the lookup value, so you can change it without editing the formula.

Step-by-Step

  1. Identify the lookup and return ranges. Decide which column contains the value you are searching for (for example, a product code in column A) and which column holds the answer you want back (for example, a price in column C). Both ranges must be the same height. If A2:A100 is the lookup array, C2:C100 is a valid return array; C2:C50 is not.
  2. Click the cell where the result should appear and type =XLOOKUP( to start the formula. Excel will show the argument names in a tooltip as you go.
  3. Enter the lookup value followed by a comma. This can be a typed value in quotes, a cell reference such as E2, or a value pulled from another formula. For example: =XLOOKUP(E2,
  4. Enter the lookup array and a comma. This is the column you are searching: A2:A100.
  5. Enter the return array and a comma. This is the column you want back: C2:C100.
  6. Skip or set the if_not_found argument. Type a comma to leave it empty, or supply a fallback such as "Not found" in quotes. Leaving it empty makes Excel return #N/A when there is no match, which is often what you want for downstream checks.
  7. Skip or set match_mode. Type a comma to accept the default of 0 (exact match). Keep 0 unless you specifically want wildcard or approximate matching, because approximate matching combined with -1 can produce surprising results.
  8. Type -1 as the sixth argument and close the bracket. The finished formula looks like: =XLOOKUP(E2,A2:A100,C2:C100,"Not found",0,-1). Press Enter.
  9. Confirm the result is the last match, not the first. Pick a lookup value that appears at least twice in your data and check that the returned row is the lower one. If it is not, you have probably put -1 in the wrong argument position.
  10. Copy the formula down or across as needed. Use absolute references ($A$2:$A$100, $C$2:$C$100) if you plan to fill the formula into other cells, so the ranges do not shift.

If you are on an older Excel build without XLOOKUP, the equivalent is an array-entered INDEX/MATCH using LOOKUP(2,1/(range=value),return_range), which also finds the last match. It is less readable but works in every version.

Common Mistakes

  • Putting -1 in the match_mode slot. The arguments run lookup_value, lookup_array, return_array, if_not_found, match_mode, search_mode. A -1 in position five means "exact match or next smaller item", not "search backwards". This is the single most common error.
  • Mismatched range sizes. If the lookup array and return array are different heights, XLOOKUP returns #VALUE!. Count the rows or click each range to confirm.
  • Assuming -1 sorts by date. It does not. It searches from the bottom of the range upward. If your newest record is at the top, -1 will return the oldest one instead.
  • Leaving gaps in the ranges. Blank rows inside the lookup array are fine for exact matching, but if you are combining -1 with a match_mode other than 0, blanks and text can trip the search.
  • Forgetting that -1 searches the whole range. It does not stop at the first blank; it keeps going to the top of the range you gave it, so keep ranges tight rather than selecting entire columns unnecessarily.

Tips

  • Use search_mode 1 (the default) when you want the first match and -1 when you want the last. Switching between them is the fastest way to sanity-check a duplicate-heavy sheet.
  • Pair -1 with a structured reference if your data is in an Excel Table, for example =XLOOKUP([@Code],Table1[Code],Table1[Price],"Not found",0,-1). The formula then adjusts automatically as rows are added.
  • If you want the newest entry by date rather than by position, sort the table by date ascending first, then -1 returns the most recent record.
  • Combine with IFNA or the if_not_found argument to keep reports clean when a lookup value is missing.
  • Test with a deliberately duplicated value before rolling the formula out across a large sheet; it takes seconds and catches argument-order mistakes immediately.

Troubleshooting

If the formula returns #NAME?, your Excel version does not support XLOOKUP. Check File > Account > About Excel for the version, and use the INDEX/MATCH alternative or open the workbook in Excel for the web, which includes XLOOKUP.

If you get #VALUE!, the lookup array and return array are almost certainly different sizes. Reselect both ranges and confirm the row counts match.

If you get #N/A for a value you can see in the column, check for trailing spaces, numbers stored as text, or hidden characters. TRIM and VALUE can clean these up, or use match_mode 2 for a wildcard match.

If the formula returns the first match instead of the last, re-check the argument order. Count the commas: the -1 must be the sixth argument, after match_mode.

If the result is correct but the wrong row appears after you sort the data, remember that -1 is positional. Sorting changes which row is last, so the returned record changes too. If that is a problem, use a date or sequence column and a MAX-based formula instead.

Frequently Asked Questions

What does search_mode -1 do in XLOOKUP?

It tells XLOOKUP to search the lookup array from the last row to the first, returning the last matching entry rather than the first.

Which argument position is search_mode?

It is the sixth and final argument: lookup_value, lookup_array, return_array, if_not_found, match_mode, search_mode.

Does search_mode -1 require the data to be sorted?

No. It simply searches bottom-up. Sorting only matters if you want the "last" match to also be the newest by date or sequence.

Can I use -1 with wildcards?

Yes, but set match_mode to 2 for wildcard matching and keep search_mode as -1. Test the result, because wildcard plus reverse search can be harder to predict.

Why does XLOOKUP return #NAME? in my workbook?

Your Excel version does not include XLOOKUP. It is available in Microsoft 365, Excel 2021 and later, and Excel for the web; older perpetual versions need INDEX/MATCH instead.

Is there a way to get the last match without XLOOKUP?

Yes. An array formula using LOOKUP(2,1/(A2:A100=E2),C2:C100) returns the last match in any Excel version, though it is harder to read and edit.