The TRIM function
TRIM strips spaces from the start and end of text and squeezes any run of spaces in the middle down to one. It's the first thing to try when lookups, COUNTIF or UNIQUE treat two identical-looking values as different.
Data pasted from emails, web pages and other systems often carries invisible spaces like these.
=TRIM(text)
Arguments
- text
- The text (or cell) to clean up.
Try it
=TRIM(A2) in B2. The two spaces before Leeds are removed.
B2
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Town as imported | Clean | ||
| 2 | Leeds | Leeds | ||
| 3 | Manchester | |||
| 4 | Milton Keynes | |||
| 5 | Leeds | |||
| 6 | ||||
| 7 |
Edit the formula in B2, or change the data, and press Enter.
More examples
| Formula | Result | What it does |
|---|---|---|
| =TRIM(A4) | Milton Keynes | Three spaces between the words become one. |
| =LEN(A3) | 12 | Manchester is 10 letters; the two trailing spaces make it 12. |
| =LEN(TRIM(A3)) | 10 | After TRIM the length is right. |
| =COUNTIF(A2:A5,"Leeds") | 1 | Only one cell matches exactly, because the other has leading spaces. Clean the column with TRIM first. |
Tips
- Check for hidden spaces by comparing
LEN(A2)withLEN(TRIM(A2)). - Clean a column in a helper column with TRIM, then paste the results back as values.
- TRIM pairs well with PROPER for names:
=PROPER(TRIM(A2)).
Common mistakes
- TRIM doesn't remove a space copied from a web page.That is usually a non-breaking space (character 160), which TRIM ignores. Replace it first:
=TRIM(SUBSTITUTE(A2,CHAR(160)," ")). - You wanted all the spaces gone, including the one between words.TRIM keeps single spaces between words. Use
SUBSTITUTE(A2," ","")to remove every space. - A VLOOKUP still fails after trimming the lookup value.The spaces may be in the table instead. Trim the lookup column as well.
Practise TRIM with instant feedback
Type real formulas into a working spreadsheet and get told straight away whether they work.