TRIM: clean up messy spaces

Lesson 24 of 54

TRIM removes spaces at the start and end of text and turns repeated spaces in the middle into single spaces. It's the first fix for data pasted from forms, emails or other systems.

Names imported from a form have extra spaces at the start, end and in the middle. In cell B2, write a formula that cleans A2.

B2
AB
1Imported nameClean name
2 Ana Ruiz
3 Luis Paz
4Marta Gil

Type in the formula bar above and press Enter to check.

Skip

How TRIM works

Syntax

=TRIM(text)
text
The cell or text to clean.

Example

A VLOOKUP returns #N/A because product codes in the export have a trailing space.

=VLOOKUP(TRIM(F2), A:C, 3, FALSE)

Trimming the search value makes "PRD-101 " match "PRD-101".

Common mistakes

  • Non-breaking spaces from web pages, which TRIM doesn't remove. Use =TRIM(SUBSTITUTE(A2, CHAR(160), " ")).
  • Leaving the TRIM formulas in place. Copy the cleaned column and paste as values, then delete the original.

Questions people ask

How do I remove all spaces, not just extra ones?

Use SUBSTITUTE: =SUBSTITUTE(A2, " ", "").

Practice it above: type the formula in the formula bar and press Enter to check.