TRIM Function Examples – Excel, VBA, & Google Sheets

Download Example Workbook

Download the example workbook

This tutorial demonstrates how to use the TRIM Function in Excel, Google Sheets, and VBA to remove all extra spaces from text.

Trim Main Function

 

How to use the TRIM Function:

The TRIM function removes all spaces in a text, except for single spaces between words.

=TRIM(B3)

Trim 01

TRIM to Remove Preceding and Trailing Spaces

It can be difficult to spot cells with trailing spaces because text is typically left-aligned:Trim 02

But if you double-click cell B3 or B4, you would see the additional spaces they have by highlighting:

Trim 02 01

Trim-02-02

Find Number of Additional Spaces

You may want to find out if the cell has additional spaces instead of doing TRIM right away.

=LEN(B3)-LEN(TRIM(B3))

Trim 03

The LEN Function returns the total number of characters in a cell. You can use that to deduct the total length of a trimmed text (removing all additional spaces).

When TRIM Doesn’t Work

You may see obvious additional spaces in the cells, but applying TRIM doesn’t seem to work.

Trim 05

That’s because these are not your typical spaces. All of our characters in the keyboard has a specific ASCII (American Standard Code for Information Interchange) code to it. You could use the CODE Function to find out what is the ASCII code of the character. For eg:

=CODE(B3)

Trim 05

The last one is a regular space in the keyboard and its ASCII code is 32. If you copy one of the “spaces” from where the TRIM doesn’t work and use CODE on it, it show 160:

Trim 07

ASCII character 160 is a non-breaking space and usually found in websites and copied to Excel. TRIM only removes character 32 (regular spaces).

To remove the non-breaking spaces and regular ones, you need to combine both the SUBSTITUTE Function and TRIM.

=TRIM(SUBSTITUTE(B3,CHAR(160),""))

Trim 08

SUBSTITUTE replaces all the non-breaking spaces (ASCII code 160) here with blanks and TRIM removes any additional regular spaces (ASCII code 32) if any.

TRIM with Number/Dates

TRIM is a text function. Upon using TRIM, the result is a text. For instance, you won’t be able to sum up these numbers in cell E3 after using TRIM.

Trim 09

To overcome the issues above, you can use the VALUE Function to convert from text to values.

Trim 11

 

TRIM VS CLEAN

The TRIM Function does not work for non-printable characters, So if you need to remove non-printable characters use the CLEAN Function. Refer to the table below to compare the two functions:

Trim Vs Clean

Useful TRIM Function Examples

You can combine TRIM with some of the Excel functions in some useful ways.

Extract Last Word from String

We can combine TRIM, RIGHT, SUBSTITUTE, and REPT to extract the last word from a string (click to see a breakdown of the formula):

Which would give us the following results:

Exact last word

Find Nth Word in the String

We can combine MID, LEN, SUBSTITUTE, and REPT with TRIM to return the nth word of the string (click to see a breakdown of the formula).

Find Nth Word

TRIM in Google Sheets

The TRIM Function works exactly the same in Google Sheets as in Excel:

Trim G Function

Additional Notes

When working with text, it’s always a good idea to use the TRIM Function to clean the text. Extra spaces can cause unwanted errors when working with functions such as XLOOKUP and MATCH.

You can use the EXACT Function to test if two strings of test are equal. Keep in mind, the EXACT, SUBSTITUTE, and FIND functions are case-sensitive, so it’s also a good idea to use the UPPER, LOWER, or PROPER functions to put all the text in a consistent case.

TRIM Examples in VBA

You can also use the TRIM function in VBA. Type:
application.worksheetfunction.trim(text)
For the function arguments (text, etc.), you can either enter them directly into the function, or define variables to use instead.