Skip to content

TRIM

Removes spaces from the start and end of text, and reduces runs of spaces between words to a single space.

Syntax

=TRIM()

Text

All versions of Excel.

Try it

Edit the formula in C2 or change the inputs. The sheet recalculates as you type.

C2
A
B
C
1
Raw name
Team
Cleaned
2
Anya Kowalski
Sales
Anya Kowalski
3
Luis Ferreira
Logistics
4
5

C2 =TRIM(A2) returns Anya Kowalski

How TRIM works

TRIM removes extra spaces from text. It deletes spaces at the start and end, and reduces any run of spaces between words to one. Data pasted from other systems, exported from the web or typed by hand often has such spaces, and they cause lookups, matches and comparisons to fail while the text looks perfectly fine.

What TRIM does

=TRIM(" Anya Kowalski ") returns Anya Kowalski. The two leading spaces and two trailing spaces are removed, and the three spaces in the middle become one.

That is all it does. It doesn't change the case of letters, and it doesn't touch other whitespace characters.

Why lookups fail without it

A trailing space is invisible but real. If a product code is "FX-1010 " in one list and "FX-1010" in another, XLOOKUP and VLOOKUP report #N/A. Compare the two cells to confirm: =TRIM(A2)=B2 returns TRUE when the only difference was extra spaces.

Fix the data by cleaning a copy, not by changing the formulas that depend on it. Put =TRIM(A2) in a helper column and copy it down. When you are sure the result is right, copy that column and paste it as values over the original.

You can also trim inside a lookup: =XLOOKUP(TRIM(A2),D2:D9,E2:E9). That fixes the lookup value, but not extra spaces in the list being searched.

The spaces TRIM cannot remove

TRIM removes the standard space, character 32. It leaves two kinds of look-alikes:

  • Non-breaking spaces, character 160, common in web pages and PDF exports. Convert them first: =TRIM(SUBSTITUTE(A2,UNICHAR(160)," ")).
  • Line breaks and tabs, characters 10 and 9, which show as breaks or gaps. CLEAN removes them. Remember that CLEAN deletes them, so words on either side of a line break join together unless you replace the break with a space first.

A full clean for data of unknown origin is =TRIM(CLEAN(SUBSTITUTE(A2,UNICHAR(160)," "))).

Numbers and TRIM

TRIM returns text. If the cell holds the text " 1250 ", the result is the text "1250". It still won't add up in a SUM. Convert it with VALUE: =VALUE(TRIM(A2)).

Applied to a real number, TRIM turns it into text. Avoid trimming cells that were already numbers.

Checking how many spaces TRIM would remove

=LEN(A2)-LEN(TRIM(A2)) counts the spaces that TRIM removes. A result above 0 means the cell has leading, trailing or repeated spaces. Use it as a quick audit across a column before you decide to clean.

TRIM in a combined formula

TRIM is often the outer layer of a join. =TRIM(A2&" "&B2&" "&C2) joins three cells with spaces and removes the extra space left when B2, the middle name, is empty. TEXTJOIN with ignore_empty set to TRUE does the same job more neatly.

Arguments

text
The text to clean. Usually a cell such as A2. TRIM removes ordinary spaces (character 32) only, not non-breaking spaces or line breaks.

Examples

Each example is a live sheet. Change the inputs or the formula and watch the result.

Example 1

Remove extra spaces from a name

C2 =TRIM(A2)

Result: Anya Kowalski

Leading and trailing spaces go, and the three spaces between the names become one.

C2
A
B
C
1
Raw name
Team
Cleaned
2
Anya Kowalski
Sales
Anya Kowalski
3
Luis Ferreira
Logistics
4
5

C2 =TRIM(A2) returns Anya Kowalski

Example 2

Test whether spaces are why two cells don't match

C2 =TRIM(A2)=B2

Result: TRUE

A2 ends in a space, so A2=B2 is FALSE. After TRIM, the two codes are equal.

C2
A
B
C
1
Entered code
Master code
Same after TRIM?
2
FX-1010
FX-1010
TRUE
3
4

C2 =TRIM(A2)=B2 returns TRUE

Example 3

Handle non-breaking spaces as well

C2 =TRIM(SUBSTITUTE(A2,UNICHAR(160)," "))

Result: Floor cleaner

The gap between the words is two non-breaking spaces. TRIM alone would leave them. SUBSTITUTE converts them to ordinary spaces first, and TRIM reduces the pair to one.

C2
A
B
C
1
Web export
Qty
Cleaned
2
Floor  cleaner
24
Floor cleaner
3
4

C2 =TRIM(SUBSTITUTE(A2,UNICHAR(160)," ")) returns Floor cleaner

Example 4

Count how many extra spaces there are

C2 =LEN(A2)-LEN(TRIM(A2))

Result: 6

The cell has 19 characters. TRIM leaves 13. The difference is 2 leading, 2 extra in the middle and 2 trailing.

C2
A
B
C
1
Raw name
Team
Extra spaces
2
Anya Kowalski
Sales
6
3
4

C2 =LEN(A2)-LEN(TRIM(A2)) returns 6

Your turn.

Solve it in the sheet. Every answer is checked, with hints if you need them.

Task 1 of 2

In B2, return the name in A2 with no leading or trailing spaces and single spaces between words. Fill it down to B5.

Answer in B2, then it fills down to B5.

B2
A
B
C
D
1
Raw name
Trimmed
Web label
Cleaned label
2
Anya Kowalski
Floor  cleaner
3
Luis Ferreira
Nitrile gloves
4
Dana Whitfield
Pallet wrap
5
Priya Nair
Safety   boots
6
7

Common mistakes

  • TRIM does not remove non-breaking spaces (character 160) from web pages or PDFs. Use SUBSTITUTE(A2,UNICHAR(160)," ") first.
  • TRIM does not remove line breaks or tabs. CLEAN removes them, but it joins the words on either side.
  • TRIM returns text. A trimmed " 1250 " is the text "1250", which SUM ignores. Wrap it in VALUE for a number.
  • Trimming a real number converts it to text. Apply TRIM to text columns only.
  • TRIM changes the copy, not the original. Put the formula in a helper column and then paste values over the original once you are happy with the result.

Questions people ask

How do I remove extra spaces in Excel?

Use `=TRIM(A2)`. It removes leading and trailing spaces and reduces multiple spaces between words to one. Copy the results and paste them as values to replace the original data.

Why does TRIM not remove the space?

The space is probably a non-breaking space, character 160, common in text copied from web pages. Use `=TRIM(SUBSTITUTE(A2,UNICHAR(160)," "))`. A line break or tab needs CLEAN.

Why does my lookup fail when the values look identical?

A hidden trailing or leading space is the usual cause. Compare `=LEN(A2)` with the length you expect, or test `=TRIM(A2)=B2`.

Does TRIM change the case of letters?

No. TRIM only deals with spaces. Combine it with PROPER, UPPER or LOWER for case, for example `=PROPER(TRIM(A2))`.

What is the difference between TRIM and CLEAN?

TRIM removes extra spaces. CLEAN removes non-printing characters such as line breaks and tabs. For messy data, use both: `=TRIM(CLEAN(A2))`.

Learn TRIM in a real sheet.

TRIM is taught in Logic and text, lesson 7: TRIM and CLEAN. Each lesson is a short, graded exercise like the one above.