Excel: Trim Function

Excel: Trim Function

by: Manuel 5 min read 0 comments Save

You paste names or invoice numbers into Excel, but your lookup returns "not found". Two cells that look identical can contain different spaces.

The "Trim" function removes leading and trailing ordinary spaces and reduces repeated spaces between words to one. Let's look at what it cleans and what needs another step.

Where to find it?

Type =TRIM( into a cell to see the formula AutoComplete tooltip. You can also find it under Formulas, in the "Text" drop-down or "Insert Function" dialog.

It works in Excel for Windows, Mac, and the web.

"Trim" and "Clean" do different jobs

"Trim" removes ordinary spaces. "Clean" removes ASCII control characters 0 through 31, including tabs and line breaks. Neither removes every invisible character.

Usage

The "Trim" function takes one argument:

=TRIM(text)
Parameter Required Type Description
text Yes Text Text in quotes or a reference to the cell to clean

Let's start with extra spaces in A1:

Data in A1
"   Sales   report   for   Maria   "

=TRIM(A1)

will return

"Sales report for Maria"

Numbers become text. With 1000 in A1, =TRIM(A1) returns the text "1000". Text is left-aligned by default, unless you've changed the cell's alignment.

A blank A1 returns an empty string (""). Logical values, which represent true or false, become text too: =TRIM(TRUE) returns "TRUE".

Dates are stored as serial numbers. Trimming a date returns that underlying number as text, rather than its displayed date format.

Errors pass straight through. If A1 contains #N/A, =TRIM(A1) also returns #N/A.

The function family

These functions help with different cleaning steps. None of the helper functions below currently has a published reference article on this site.

Function What it does
Trim Removes leading, trailing, and repeated ordinary spaces
Clean Removes ASCII control characters 0 through 31
Substitute Replaces specified text or characters
Value Converts text representing a number into a number

Real-world examples

Cleaning names before a lookup

João exports customer names with leading spaces into column A. A2 contains the name to find. Customers is an Excel table with clean Name and Email columns. The references below select those columns.

=XLOOKUP(TRIM(A2), Customers[Name], Customers[Email])

This returns the matching email. The "Xlookup" function requires Microsoft 365 or a supported newer version, such as 2021 or 2024. It isn't available in 2016 or 2019.

Comparing two columns

When we trim both values, ordinary spacing differences no longer affect the comparison. The "If" function reports the result:

=IF(TRIM(A2)=TRIM(B2), "Match", "Check")

Preparing text numbers for a total

For amounts in A2:A10, enter this in B2 and fill down to B10:

=VALUE(TRIM(A2))

Then total B2:B10 with the "Sum" function:

=SUM(B2:B10)

The helper column works in older versions without array-formula entry.

Non-intuitive behaviors

Non-breaking spaces survive

Ordinary spaces have character code 32. Web pages and PDFs can contain non-breaking spaces, code 160, which look similar but stay after trimming. The "Char" function returns the character specified by its code.

Replace non-breaking spaces first

Use =TRIM(SUBSTITUTE(A1, CHAR(160), " ")) to replace them with ordinary spaces, then trim.

Source

Microsoft's TRIM documentation explains the difference between these spaces.

Numbers become text

Check totals after cleaning

Functions such as "Sum" ignore text in referenced cells. Convert trimmed numeric text with =VALUE(TRIM(A1)) before totaling it. Arithmetic operators can convert numeric text automatically, so their behavior differs from summing a range.

Tabs and line breaks remain

Cleaning can join words

=TRIM(CLEAN(A1)) removes tabs and line breaks without adding spaces. If they separate words, replace them with spaces first, for example =TRIM(SUBSTITUTE(A1, CHAR(10), " ")) for line feeds.

Source

Microsoft's data-cleaning guide describes the characters handled by "Trim", "Clean", and "Substitute".

Limitations

It only removes ordinary spaces

For other whitespace, identify the character and replace it with the "Substitute" function. Use "Clean" for ASCII control characters.

It returns text

Convert numeric results with the "Value" function when calculations need numbers. Leave names and identifiers as text.

It takes one argument

You can't supply several separate text arguments. Versions supporting dynamic arrays can process =TRIM(A2:A10) and spill the results into neighboring cells. In older versions, enter =TRIM(A2) and fill down.

Source

Microsoft's array compatibility guide explains differences between versions.

Localization

"Trim" has one argument, but nested formulas need argument separators. Use the form matching your regional settings. Localized installations may also use translated function names.

With comma separators:

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

With semicolon separators:

=TRIM(SUBSTITUTE(A1; CHAR(160); " "))

Troubleshooting Common Errors

Spaces remain after trimming

Cause: The remaining character may be a non-breaking space.

Solution: Try =TRIM(SUBSTITUTE(A1, CHAR(160), " ")). If it remains, identify the character before replacing it.

The total ignores cleaned numbers

Cause: The cleaned cells contain text.

Solution: Use the "Value" function in a helper column, as shown in the total example above.

A lookup still can't find a name

Cause: Either side may contain extra spaces.

Solution: Clean both the search value and the lookup column. Using the Customers table introduced earlier:

=XLOOKUP(TRIM(SUBSTITUTE(A2, CHAR(160), " ")), TRIM(SUBSTITUTE(Customers[Name], CHAR(160), " ")), Customers[Email])

Text still appears on two lines

Cause: The cell may contain a line break or use Wrap Text formatting.

Solution: Replace embedded line feeds with =TRIM(SUBSTITUTE(A1, CHAR(10), " ")). If wrapping causes the display, adjust the column width or turn off Wrap Text.

Recommendations

Check imported spaces

For web or PDF data, check for non-breaking spaces. Use the "Substitute" wrapper when they're present.

Convert numeric text when needed

Use "Value" before totaling cleaned amounts. Keep identifiers as text when leading zeros matter.

Clean once, then paste as values

For a one-off import, clean in a helper column. Check the results before copying them over the original with Paste Special, Values.

Always deal with errors

If blank output is appropriate, use the "Iferror" function:

=IFERROR(TRIM(A1), "")

Otherwise, use a visible message such as "Check source" so errors remain easy to find.

Final Thoughts

The "Trim" function is useful for ordinary spacing differences. Replace other characters when needed, and convert cleaned amounts back to numbers before totaling them.

Sources

Back to the Excel Function Reference

Photo by Collab Media on Unsplash

Comments

Spotted a mistake or have a better approach? Let me know. I read and reply to every one.

💬

No comments yet

Be the first to share your thoughts on this article!

Leave a Comment

All comments are reviewed for spam before being displayed. Wrap code in `backticks`. 5000 left
Replying to