Find the Address of the Last Used Cell in Google Sheets

Published on

The “last used cell” in Google Sheets refers to the cell with the highest row or column number that is not empty within a specified range. It is not related to the time the cell was last edited. There are no built-in functions in Google Sheets to directly find the address of the last used cell within a range. However, here are two dynamic formulas you can use: one for vertical datasets and another for horizontal datasets.

To find the address of the last used cell in a vertical dataset, use the following formula:

=LET(range, you_range_here, test1, IFNA(BYCOL(range, LAMBDA(col, CELL("row", INDEX(col, XMATCH(TRUE, col<>"", 0, -1)))))), test2, IFNA(BYCOL(range, LAMBDA(col, CELL("address", INDEX(col, XMATCH(TRUE, col<>"", 0, -1)))))), fnl, XLOOKUP(MAX(test1), test1, test2, "", 0, -1), fnl)

To find the last used cell in a horizontal dataset, use this formula:

=LET(range, you_range_here, test1, IFNA(BYROW(range, LAMBDA(row, CELL("col", INDEX(row, 0, XMATCH(TRUE, row<>"", 0, -1)))))), test2, IFNA(BYROW(range, LAMBDA(row, CELL("address", INDEX(row, 0, XMATCH(TRUE, row<>"", 0, -1)))))), fnl, XLOOKUP(MAX(test1), test1, test2, "", 0, -1), fnl)

In both formulas, replace your_range_here with the range you want to apply these formulas.

Finding the Address of the Last Used Cell in a Vertical Dataset

In the following example, I have sales data in the range A1:D as shown below:

Address of the Last Used Cell in a Vertical Dataset

The following formula returns $B$6, which is the address of the last used cell in this range:

=LET(range, A1:D, test1, IFNA(BYCOL(range, LAMBDA(col, CELL("row", INDEX(col, XMATCH(TRUE, col<>"", 0, -1)))))), test2, IFNA(BYCOL(range, LAMBDA(col, CELL("address", INDEX(col, XMATCH(TRUE, col<>"", 0, -1)))))), fnl, XLOOKUP(MAX(test1), test1, test2, "", 0, -1), fnl)

If you want to find the last used cell in any specific column within this range (e.g., column D), replace A1:D with D1:D.

Formula Breakdown

The core of the formula lies in the combination of XMATCH, INDEX, and CELL functions.

XMATCH(TRUE, col<>"", 0, -1)

This XMATCH formula matches TRUE in an array that contains TRUE or FALSE values (indicating non-empty cells) from bottom to top and returns the position of the first match in the array.

We use this function within BYCOL, so col refers to each column in the range A1:D. This means the test is performed on each column, and the formula returns the relative position of each column’s last non-empty cell.

The INDEX function then returns the value of those cells.

There are two BYCOL formulas named test1 and test2 (defined using LET). In both BYCOL formulas, we use the INDEX result. The CELL function in test1 returns the row numbers, while the CELL function in test2 returns the cell addresses.

Finally, the XLOOKUP function:

XLOOKUP(MAX(test1), test1, test2, "", 0, -1)

This formula looks for the maximum value in test1 (which contains the row numbers) and returns the corresponding cell address from test2, giving you the address of the last used cell in the specified range.

Finding the Address of the Last Used Cell in a Horizontal Dataset

Although people commonly use vertical datasets, they sometimes use horizontal datasets.

For example, consider the following dataset in A3:Z6, where the first column contains employee names and the rest of the range represents their attendance.

Address of the Last Used Cell in a Horizontal Dataset

The following formula will return the address of the last used cell in the range A3:Z6:

=LET(range, A3:Z6, test1, IFNA(BYROW(range, LAMBDA(row, CELL("col", INDEX(row, 0, XMATCH(TRUE, row<>"", 0, -1)))))), test2, IFNA(BYROW(range, LAMBDA(row, CELL("address", INDEX(row, 0, XMATCH(TRUE, row<>"", 0, -1)))))), fnl, XLOOKUP(MAX(test1), test1, test2, "", 0, -1), fnl)

If you want to find the address of the last used cell in a particular row, such as A3:Z3, simply replace A3:Z6 with A3:Z3. It’s that easy!

How Does This Formula Differ from the Vertical Data Formula?

Here are the key differences between the formula that returns the cell address of the last used cell for horizontal and vertical datasets:

  1. XMATCH Function: In the horizontal formula, XMATCH matches TRUE in each row and returns the position of the last non-empty cell. In the vertical formula, it matched TRUE in each column.
  2. BYROW vs. BYCOL: The horizontal formula uses the BYROW function, while the vertical formula uses BYCOL.
  3. INDEX Function: The horizontal formula’s INDEX offsets columns to return values, whereas the vertical formula’s INDEX offsets rows.
  4. CELL Function: In the horizontal formula, CELL returns column numbers, while in the vertical formula, it returns row numbers.

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Hierarchical Numbering Sequences in Excel

Creating hierarchical numbering sequences in an Excel spreadsheet can significantly improve the way you...

How to Easily Repeat a Sequence of Numbers in Excel

Excel offers multiple ways to accomplish tasks, and the simplicity of each approach depends...

Create a Sequence of Dates at Every Nth Row in Excel (Dynamic Array)

Would you like to create a sequence of dates in every nth row in...

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.