Text and cleaning · About 5 minutes
Remove extra spaces with TRIM
Data copied from a website or another system often carries hidden spaces: Ramesh Gupta or Suresh Babu. You can't see them, but they stop lookups and counts from matching.
TRIM removes spaces at the start and the end, and turns any run of spaces inside the text into a single space. =TRIM( reads as "A2 without the extra spaces".
You can put TRIM inside another function. =PROPER( reads as "remove the extra spaces from A2, then give each word a capital". The inside runs first.
Part of Plus
Free during our founding launch: sign in to unlock this lesson's exercises, the timed practice sets and the rest of Plus. The free lessons stay free, with no account needed.
What TRIM does
TRIM removes extra spaces from text. It deletes every space at the start and the end, and turns any run of spaces between words into a single space.
Extra spaces are hard to see but cause real trouble. Text pasted from a website, a PDF or another system often carries them, and then a lookup can't find a name, a count misses a row, or two cells that look the same don't match. TRIM in a new column gives you clean text to work with.
How to write TRIM
=TRIM(
- text
- The cell to clean, like A2. TRIM gives back the same text with only single spaces between the words.
TRIM worked example
City names pasted from a web page have extra spaces. In C2, clean the name in B2, then fill down to C5.
| A | B | C | |
|---|---|---|---|
| 1 | Store | Pasted | Clean |
| 2 | S-11 | Lagos | Lagos |
| 3 | S-12 | New York | New York |
| 4 | S-13 | Rome | Rome |
| 5 | S-14 | San Jose | San Jose |
=TRIM( B2)Take B2 without its extra spaces: the two spaces before the name go, so the first row shows Lagos. Filled down, the column shows New York, Rome and San Jose, each with one space at most between the words.Common TRIM mistakes
TRIM leaves a space behind
Text copied from a web page often has a non-breaking space, a special space TRIM does not remove in Excel or Google Sheets. In Excel, swap it for a normal space first: TRIM(.
The single spaces between words are still there
That is TRIM working as planned: it keeps one space between words. To remove every space, use SUBSTITUTE to swap each space for nothing.
The lookup still can't find the clean name
Clean both sides. If the list you look in has extra spaces too, add a TRIM column there as well, and look up the clean column.
TRIM, CLEAN or SUBSTITUTE?
Use TRIM for extra spaces at the start, at the end and between words. It works the same way in Excel and Google Sheets.
Use SUBSTITUTE to remove every space, or any other character, like a dash. CLEAN removes hidden characters that are not printed, like line breaks inside a cell. You can nest them, with the inside running first, when pasted data has more than one problem.
Questions people ask
What does TRIM do in Excel?
It removes spaces at the start and end of the text, and turns double spaces between words into one.
Why is TRIM not removing spaces?
They're probably non-breaking spaces, common in text copied from websites. Swap them first: =TRIM(.
How do I remove all spaces, not just extra ones?
Use =SUBSTITUTE(. It removes every space, including those between words.
How do I trim a whole column?
Put =TRIM( in a helper column, fill it down, then paste the results as values over the original.
Is there a trim option in Google Sheets without a formula?
Yes. Select the cells, then choose Data > Data cleanup > Trim whitespace.