Dynamically Remove Last Empty Rows and Columns in Sheets

At the time of writing, there’s no TRIMRANGE function in Google Sheets. Sure, we have a function called ARRAY_CONSTRAIN that can trim a range, but it’s not dynamic—you have to manually specify how many rows and columns to return.

So how do you dynamically remove the last (trailing) empty rows and columns from a range? You can use the following formula in Google Sheets:

=LET(
  range, array_or_range,
  ARRAY_CONSTRAIN(
    range,
    MAX(BYCOL(range, LAMBDA(col, ArrayFormula(XLOOKUP("*?", col&"", SEQUENCE(ROWS(col)), , 2, -1))))),
    MAX(BYROW(range, LAMBDA(row, ArrayFormula(XLOOKUP("*?", row&"", SEQUENCE(1, COLUMNS(row)), , 2, -1)))))
  )
)

Here, range refers to the data range or formula result you want to trim.

Just replace 'array_or_range' with your actual range or array formula. The rest of the formula will dynamically calculate the number of rows and columns to retain—cutting off the trailing blanks.

Why Remove Trailing Empty Rows and Columns?

When using open-ended ranges in functions like FILTER, SORT, QUERY, or IMPORTRANGE, you might end up with extra empty rows or columns at the end.

This can be problematic. For example, if you enter any data in an empty column next to the result range, the formula might break—because that empty column is technically part of the result.

So, trimming out those last empty rows and columns dynamically is a smart and practical fix.

Example 1: Dynamically Remove Last Empty Rows and Columns from a Physical Range

Suppose you want to trim the range A2:Z in Sheet1 from another sheet (Sheet2) using a formula. You can use:

=LET(
  range, Sheet1!A2:Z,
  ARRAY_CONSTRAIN(
    range,
    MAX(BYCOL(range, LAMBDA(col, ArrayFormula(XLOOKUP("*?", col&"", SEQUENCE(ROWS(col)), , 2, -1))))),
    MAX(BYROW(range, LAMBDA(row, ArrayFormula(XLOOKUP("*?", row&"", SEQUENCE(1, COLUMNS(row)), , 2, -1)))))
  )
)

Let’s say your sample data is in A2:I8. This formula will dynamically remove trailing blanks—no manual trimming needed.

Example of Dynamically Removing Trailing Empty Rows and Columns in Google Sheets

Example 2: Dynamically Remove Last Empty Rows and Columns from an Expression

Say you’re importing data using this IMPORTRANGE formula:

=IMPORTRANGE("https://docs.google.com/spreadsheets/d/1BT6Rhtme1j1E2uMVdZP3skC5_N2U5LOg_tmSYVy6Pd8/edit?gid=345964233", "Sheet1!A2:Z")

To dynamically remove the last empty rows and columns from the result, simply wrap it with our custom trimming formula:

=LET(
  range, IMPORTRANGE("https://docs.google.com/spreadsheets/d/1BT6Rhtme1j1E2uMVdZP3skC5_N2U5LOg_tmSYVy6Pd8/edit?gid=345964233", "Sheet1!A2:Z"),
  ARRAY_CONSTRAIN(
    range,
    MAX(BYCOL(range, LAMBDA(col, ArrayFormula(XLOOKUP("*?", col&"", SEQUENCE(ROWS(col)), , 2, -1))))),
    MAX(BYROW(range, LAMBDA(row, ArrayFormula(XLOOKUP("*?", row&"", SEQUENCE(1, COLUMNS(row)), , 2, -1)))))
  )
)

Formula Breakdown

The two custom unnamed LAMBDA functions are the core of this formula. One finds the last non-empty row number, and the other finds the last non-empty column number.

Then, ARRAY_CONSTRAIN trims the range based on those values.

Key Parts:

  • MAX(BYCOL(...)): Returns the last row number that contains a value.
  • MAX(BYROW(...)): Returns the last column number that contains a value.

Syntax Reminder – ARRAY_CONSTRAIN

ARRAY_CONSTRAIN(input_range, num_rows, num_cols)
  • input_range: The actual range or array expression you want to trim.
  • num_rows: Dynamically calculated number of non-empty rows.
  • num_cols: Dynamically calculated number of non-empty columns.

Final Thoughts

This is a solid solution if you’re looking to dynamically remove last empty rows and columns in Google Sheets—whether it’s from a physical range or an expression like IMPORTRANGE. It saves time and makes your formulas more resilient.

Related 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.

How to Sort Pie Slices in Google Sheets

To sort pie slices in a pie chart, you need to sort the data...

Filter Items Unique to Groups in Google Sheets

In this tutorial, we'll learn how to filter items unique to groups in Google...

Find Common Items Across Multiple Columns in Google Sheets

This tutorial explains how to find common items across multiple columns in Google Sheets....

Sort Column by Length of Text in Google Sheets

To sort a column by length of text, you can either use the QUERY...

More like this

How to Sort Pie Slices in Google Sheets

To sort pie slices in a pie chart, you need to sort the data...

Filter Items Unique to Groups in Google Sheets

In this tutorial, we'll learn how to filter items unique to groups in Google...

Find Common Items Across Multiple Columns in Google Sheets

This tutorial explains how to find common items across multiple columns in Google Sheets....

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.