Dynamically Remove Last Empty Rows and Columns in Sheets

Published on

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 Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Top Discussions

More like this

Pivot Table Formatting, Output & Special Behavior in Google Sheets

Pivot Tables in Google Sheets are powerful—but they can get tricky once you move...

Pivot Table Calculations & Advanced Metrics in Google Sheets

When it comes to built-in tools for data analysis and visualization in Google Sheets,...

Google Sheets Pivot Table Tutorial: Basics, Setup, and Date Grouping

The easiest way to summarize, analyze, and report data in Google Sheets is by...

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.