Excel Formulas to Find the Last Occurrence in a Row

📅 May 17, 2026 📝 Sarah Miller

Locating the most recent update in a horizontal dataset can be highly frustrating. Traditional lookup methods, such as standard HLOOKUP or basic MATCH, inherently default to retrieving the first match rather than the last. Fortunately, leveraging advanced vector arrays grants users immediate access to the most recent data point, bypassing tedious manual sorting. As a crucial stipulation, the chosen formula must ignore empty cells to prevent inaccurate, blank returns. For instance, the robust construction =LOOKUP(2, 1/(A1:Z1<>""), A1:Z1) successfully isolates the final non-blank entry. Below, we break down this syntax step-by-step to optimize your data retrieval.

Excel Formulas to Find the Last Occurrence in a Row

In data analysis, spreadsheets are frequently designed horizontally. From monthly budget trackers and project timelines to sales pipelines and attendance rosters, data often flows from left to right across columns in a single row. When managing these horizontal layouts, a common challenge arises: how do you find the last occurrence of a value, or the last non-blank entry, in a row?

Whether you need to extract the most recent month's sales figure, identify the latest status update of a project, or locate the column position of the last recorded entry, Excel offers several powerful formulas to get the job done. This comprehensive guide will walk you through the best methods to search for the last occurrence in a row, ranging from modern Microsoft 365 solutions to classic backward-compatible workarounds.

Understanding the Scenario

To illustrate these solutions, let us assume a common scenario: a project tracking sheet where Row 2 contains status updates across columns B through M (representing January through December). Some cells contain updates like "In Progress," "Pending," or "Completed," while others remain empty because the months have not yet occurred.

Our goal is to write a formula that scans this row from right to left (December to January) to find either:

  • The last non-blank value (the most recent update).
  • The last occurrence of a specific value (e.g., the last time the status was "Pending").
  • The column position of that last occurrence.

Method 1: The Modern Standard – XLOOKUP

If you are using Microsoft 365, Excel 2021, or Excel for the Web, XLOOKUP is the most efficient, intuitive, and powerful function for this task. By default, standard lookup functions search from left to right (first to last). However, XLOOKUP features a dedicated argument that allows you to reverse the search direction.

The Syntax of XLOOKUP

=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])

To search from last to first (right to left in a row), we set the search_mode argument to -1.

Scenario A: Finding the Last Occurrence of a Specific Value

If you want to find the last column in row 2 (columns B through M) that contains the specific text "Completed", use this formula:

=XLOOKUP("Completed", B2:M2, B2:M2, "Not Found", 0, -1)

How it works:

  • "Completed": The value you are searching for.
  • B2:M2: The row range to search.
  • B2:M2: The row range from which to return the result.
  • "Not Found": The fallback value if "Completed" is not in the row.
  • 0: Specifies an exact match.
  • -1: Instructs Excel to perform a reverse search (from right to left, starting at column M).

Scenario B: Finding the Last Non-Blank Cell Using XLOOKUP

To find the absolute last entered value in a row, regardless of what that value is, we can use a logical expression inside XLOOKUP:

=XLOOKUP(TRUE, B2:M2<>"", B2:M2, "", 0, -1)

How it works:

  • B2:M2<>"" creates an array of TRUE and FALSE values (TRUE if the cell is not empty, FALSE if it is empty).
  • XLOOKUP searches for the value TRUE within this array.
  • Because search_mode is set to -1, it starts checking from right to left, instantly returning the value of the first non-blank cell it encounters.

Method 2: The Classic Masterclass – LOOKUP (Universal Compatibility)

If you need your spreadsheet to work across older versions of Excel (such as Excel 2019, 2016, or 2013), the classic LOOKUP function is your best option. It is incredibly robust, requires no array entry shortcuts (like Ctrl+Shift+Enter), and is remarkably concise once you understand its underlying logic.

The Formula

To find the last non-empty value in the row range B2:M2, use the following formula:

=LOOKUP(2, 1/(B2:M2<>""), B2:M2)

How It Works (The Math Behind the Magic)

This formula relies on a brilliant exploitation of Excel's binary search algorithm and error handling:

  1. The Logical Array: The expression B2:M2<>"" evaluates every cell in the row. If a cell contains data, it returns TRUE; if empty, it returns FALSE. This produces an array like: {TRUE, TRUE, FALSE, TRUE, FALSE, ...}.
  2. The Division: We divide 1 by this array: 1/(B2:M2<>"").
    • In Excel arithmetic, TRUE acts as 1, and FALSE acts as 0.
    • 1 / TRUE becomes 1 / 1, which equals 1.
    • 1 / FALSE becomes 1 / 0, which results in a division error: #DIV/0!.
    • The resulting array looks like this: {1, 1, #DIV/0!, 1, #DIV/0!, ...}.
  3. The Approximate Match Trick: The LOOKUP function is designed to search for a lookup value in an array. If it cannot find an exact match, it matches the largest value in the lookup array that is less than or equal to the lookup value.
  4. By searching for the number 2 (which is strictly greater than any value in our array of 1s and errors), and because LOOKUP automatically ignores error values like #DIV/0!, Excel scans the entire row. Finding no 2, it settles on the position of the last numerical value (1) in the array.
  5. Once it identifies this position, it returns the corresponding value from the vector array (our third argument, B2:M2).

Finding the Last Occurrence of a Specific Value with LOOKUP

If you want to find the last occurrence of a specific string (e.g., "Pending") instead of just any non-blank cell, you can modify the logical expression slightly:

=LOOKUP(2, 1/(B2:M2="Pending"), B2:M2)

This evaluates only the cells containing "Pending" to 1, and all other cells (empty or containing other values) to #DIV/0!, successfully returning the last "Pending" entry from the row.


Method 3: Finding the Column Index of the Last Occurrence

Sometimes, simply retrieving the value of the last occurrence is not enough; you may need to know *where* that occurrence is. For instance, you might want to know the column number or column header (e.g., "October") of the last entry.

To Find the Relative Column Number

If you want to find the relative column index (e.g., 1 for B, 2 for C, etc.) where the last non-empty cell is located, you can combine MATCH with the same logic used in the LOOKUP formula:

=MATCH(2, 1/(B2:M2<>""))

This returns a number representing the column position relative to your range. If the last non-blank entry is in column G (the 6th column in the B:M range), the formula will return 6.

To Find the Header of the Last Column

If row 1 contains your column headers (e.g., "Jan", "Feb", "Mar", etc.), you can feed the position index back into an INDEX function to extract the header name of the last occurrence:

=INDEX(B1:M1, MATCH(2, 1/(B2:M2<>"")))

Alternatively, using XLOOKUP makes this remarkably clean:

=XLOOKUP(TRUE, B2:M2<>"", B1:M1, "", 0, -1)

Summary Comparison of Methods

To help you choose the best formula for your workbook, review this summary of when to use each approach:

Formula Method Excel Compatibility Best For Example Formula
XLOOKUP Excel 365, 2021+ Modern spreadsheets, simple setups, native right-to-left searches. =XLOOKUP(TRUE, B2:M2<>"", B2:M2, "", 0, -1)
LOOKUP (Vector) All Versions Legacy support, robust calculation engine, clean non-array execution. =LOOKUP(2, 1/(B2:M2<>""), B2:M2)
INDEX / MATCH All Versions Returning headers or positions associated with the last occurrence. =INDEX(B1:M1, MATCH(2, 1/(B2:M2<>"")))

Pro-Tip: Managing Error Handling

If your target row is completely empty, both LOOKUP and XLOOKUP formulas will return errors (#N/A). To keep your reports looking polished and professional, wrap your formulas in the IFERROR function to display a custom blank space or descriptive text instead of an ugly error message:

=IFERROR(LOOKUP(2, 1/(B2:M2<>""), B2:M2), "No Data Entered")

Conclusion

Finding the last occurrence in a row is an incredibly common challenge when dealing with dynamic dashboards and chronological tracking sheets. If you are using modern Microsoft 365, leverage the elegant XLOOKUP with its -1 search direction argument. If you are working in environments that require backward compatibility, rely on the time-tested LOOKUP(2, 1/(range<>""), range) vector trick. By mastering these formulas, you can automate your reporting pipelines, reduce manual updates, and ensure your summaries always reflect the most up-to-date data available.

Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.