Excel Function: TRIMRANGE

The TRIMRANGE function removes empty rows or columns around a data range.

Even more convenient, we’ll see below that this function can be simply replaced by one or two . when entering a cell range reference.

Usage:

=TRIMRANGE(range)

or

=TRIMRANGE(range, row_trim_mode, col_trim_mode)


Usage example

A sample range has been defined; the goal here is to remove empty rows and columns:

excel trimrange

The arguments of the TRIMRANGE function are as follows:

In this case, we’ll use the default behavior (i.e. remove empty rows and columns at the beginning and end), so there’s no need to specify optional arguments:

=TRIMRANGE(A1:D17)

excel function trimrange

Empty rows and columns have been successfully removed.

However, this function has an inconvenient behavior when the range contains empty cells, as it displays 0 in their place.

Removing the 0s

To remove these annoying 0s, there are several solutions. Choose the one that is most appropriate based on your use case if you need to get rid of the 0s.

Conditional formatting

The 0s are hidden here using conditional formatting (by coloring them white):

excel function trimrange remove zeros conditional formatting png

CLEAN function

The 0s are removed here using the CLEAN function (which does not remove 0s originally present):

=CLEAN(TRIMRANGE(A1:D17))

excel function trimrange remove zeros clean

The CLEAN function is a simple solution but it converts numbers to text, which is not always desirable.

Other formulas

Other formulas allow you to remove 0s without modifying the data. Here are 2 examples:

=LET(a,TRIMRANGE(A1:D17),IF(ISBLANK(a),"",a))
=MAP(TRIMRANGE(A1:D17),LAMBDA(c,IF(ISBLANK(c),"",c)))

These formulas produce the following result:

excel function trimrange remove zeros formula

Using dots

The TRIMRANGE function can be replaced by adding one or two . in the reference:

=A1.:D17Removes empty rows and columns at the beginning
=A1:.D17Removes empty rows and columns at the end
=A1.:.D17Removes empty rows and columns at the beginning and end

For example:

excel function trimrange dots

Dropdown list

If column A contains a variable number of data entries for a dropdown list, you can obtain the data range using this simple formula:

=A:.A

excel function trimrange dropdown list

Then create your dropdown list by entering this same formula as the source:

excel function trimrange dropdown list validation

To get:

excel function trimrange dot dropdown list

Note: this function is only available with Office 365 (or a sufficiently recent version of Excel).