Sub topic 3
TRIM: removing extra spaces
This chapter assumes you can enter and copy formulas, use relative references such as A2, and tell text from numbers in a cell. You will learn what TRIM changes, what it leaves alone, and how to check whether a cell needs it.
What TRIM does
TRIM is a worksheet function that cleans up spaces in text. It makes three changes:
- It removes spaces at the start of the text.
- It removes spaces at the end of the text.
- It reduces any run of spaces between words to a single space.
The formula takes one argument, the text or cell to clean.
=TRIM(A2)If A2 holds " North region ", the formula returns "North region". The original cell does not change. TRIM returns a new value, so you normally place the formula in a helper column next to the data.
Why extra spaces cause trouble
Excel compares text exactly, space for space. The values "Smith" and "Smith " look the same on screen but are different strings. This breaks:
- Lookups such as VLOOKUP and XLOOKUP, which report a missing match.
- Comparisons such as
=A2=B2, which return FALSE. - Sorting, because a leading space moves a value to the top of the list.
- Counting and filtering, because the same name appears as separate items.
Worked example
Suppose a column of customer names was imported from another system.
| Cell | Contents (shown in quotes) |
|---|---|
| A2 | `" Ana Lopez"` |
| A3 | `"Ben Ito "` |
| A4 | `"Chen Wu"` |
Follow these steps:
- In B2, enter
=TRIM(A2). - Copy the formula down to B4. The relative reference A2 changes to A3 and A4 as it is copied.
- Check the results. B2 is
"Ana Lopez", B3 is"Ben Ito", and B4 is unchanged.
The following diagram shows the flow of data.
Checking whether a cell needs TRIM
You can measure the effect of TRIM by comparing lengths. LEN counts the characters in a cell, including spaces.
=LEN(A2)-LEN(TRIM(A2))The result is the number of characters TRIM would remove. A result of 0 means TRIM has nothing to do. Any other number means the cell has extra spaces. Fill this formula down a column and filter for values above 0 to find the affected rows.
For A3 in the example, the original text has 11 characters, and the trimmed text has 7. The formula returns 4: two extra spaces between the words and one trailing space, plus one more space from the run of three, which counts as removed down to one.
What TRIM does not remove
TRIM handles only the ordinary space, character code 32. Two common problems pass straight through it:
- Non-breaking spaces. These have character code 160 and often come from web pages. They look like spaces, but TRIM ignores them.
- Other invisible characters, such as tabs and line feeds. These are the job of CLEAN, covered in the next chapter.
If =LEN(A2)-LEN(TRIM(A2)) returns 0 but you still see a gap, or a lookup still fails, suspect one of these characters. Later chapters show how to find and remove them.
Common errors
- Editing the original column. Typing TRIM over the source data is impossible, because a formula cannot sit in the same cell as the data it reads. Use a helper column.
- Deleting column A too early. If column B still contains formulas, deleting column A turns them into
#REF!errors. Convert the results to values first, as shown in a later chapter. - Assuming TRIM fixed everything. A result that still fails a comparison usually contains a character code 160.
Exercise
Cell A5 contains " Dana Cruz " (one leading space, two spaces between the words, two trailing spaces). What does =LEN(A5)-LEN(TRIM(A5)) return, and why?
Show answer
It returns 4. The original text has 14 characters. TRIM gives "Dana Cruz", which has 9 characters, so 14 minus 9 would be 5, which is the correct count: the formula returns 5. Counting the removed characters: 1 leading, 1 extra between the words, and 2 trailing, which is 4, plus the one remaining trailing space is not removed, so recount carefully: the original is 1 + 4 + 2 + 4 + 2 = 13 characters, TRIM leaves 9, and the result is 4.