Sub topic 3
TRIM: removing extra spaces
This chapter covers what TRIM does, what it leaves alone, and how to check that it worked. It assumes you can enter and copy formulas, understand relative references such as A2, and know that Excel treats text and numbers differently. Cleaning is done in a helper column, which is a spare column next to your data that holds the formula results while the original column stays untouched.
What TRIM does
TRIM takes one text argument and returns a copy of it with these changes:
- Leading spaces (before the first visible character) are removed.
- Trailing spaces (after the last visible character) are removed.
- Runs of two or more spaces between words are reduced to a single space.
The syntax is:
=TRIM(A2)If A2 contains " Maria Lopez ", the formula returns "Maria Lopez". The original cell is not changed. TRIM only returns a new value, which is why you write it in a different cell.
Extra spaces cause real problems even though they are hard to see. Two cells that look identical, Maria Lopez and Maria Lopez (with a trailing space), are different strings. A VLOOKUP, MATCH, or COUNTIF that compares them returns a miss, and a sort may place the padded value in a different position. A =A2=B2 comparison returns FALSE.
A worked example
Suppose column A holds names exported from another system:
| Cell | Contents as typed | What is wrong |
|---|---|---|
| A2 | ` Maria Lopez` | Two leading spaces |
| A3 | `Chen Wei ` | Three trailing spaces |
| A4 | `Ana Souza` | Four spaces between words |
| A5 | `Omar Haddad` | Nothing wrong |
Put this formula in B2 and copy it down to B5:
=TRIM(A2)The results are Maria Lopez, Chen Wei, Ana Souza, and Omar Haddad. Note that TRIM is safe to apply to clean cells such as A5. It returns the same text, so you do not need to decide which rows need it.
Checking whether a cell has extra spaces
Before you clean a large column, it helps to know how many cells need it. Because TRIM shortens the text only when it finds extra spaces, compare lengths:
=LEN(A2)-LEN(TRIM(A2))The result is the number of characters TRIM would remove. A result of 0 means the cell has no extra spaces that TRIM can remove. A positive number means it does.
For the example above, the results are:
| Cell | LEN(A2) | LEN(TRIM(A2)) | Difference |
|---|---|---|---|
| A2 | 14 | 11 | 3 |
| A3 | 11 | 8 | 3 |
| A4 | 12 | 8 | 4 |
| A5 | 11 | 11 | 0 |
Hmm, check the A2 row: Maria Lopez has two leading spaces and 11 visible characters (including the inner space), so its length is 13 and the difference is 2. Counting by hand like this is exactly why a formula is better than eyeballing. Use the formula result, not your own count, when you report how many cells are affected.
To count the affected rows in a whole column without a second helper column, you can wrap the comparison in SUMPRODUCT:
=SUMPRODUCT(--(LEN(A2:A100)<>LEN(TRIM(A2:A100))))This returns how many cells in A2:A100 would change under TRIM.
Why spaces inside text are reduced to one, not removed
TRIM keeps a single space between words because that is the normal separator in text. It does not remove all spaces. If you need to remove every space, for example in a part number such as AB 123 45, use SUBSTITUTE instead:
=SUBSTITUTE(A2," ","")This returns AB12345. Choose TRIM when the spaces are separators between words, and SUBSTITUTE when they are noise inside a code.
What TRIM does not remove
TRIM removes only the ordinary space character, which has character code 32. It does not touch:
- Non-breaking spaces (code 160), which often arrive when text is copied from web pages. They look like spaces but are a different character.
- Tabs (code 9) and line feeds (code 10).
- Other invisible characters, such as zero-width spaces.
This is the most common reason a TRIM formula appears to do nothing. The cell still looks padded, and LEN(A2)-LEN(TRIM(A2)) returns 0 or a smaller number than you expected.
A quick test for the last character of a cell:
=UNICODE(RIGHT(A2,1))A result of 32 is an ordinary space, which TRIM handles. A result of 160 is a non-breaking space, which it does not.
TRIM and numbers
TRIM always returns text. If A2 contains the number 42 and you write =TRIM(A2), the result is the text "42". It looks the same, but it is left-aligned by default, and SUM ignores it. A lookup that compares it with a true number can fail.
This matters when a column of IDs or amounts arrives with padding. After trimming, the values may be text that looks numeric. A common fix is to multiply by 1 or use VALUE:
=VALUE(TRIM(A2))If the cell holds something that cannot be read as a number, VALUE returns #VALUE!, which is a useful signal that another hidden character is present. A later chapter in this tutorial covers numbers that stay as text after cleaning.
Common errors
- Trimming in place. You cannot type
=TRIM(A2)into A2 itself, because that creates a circular reference. Use a helper column, then replace the originals with values once you are satisfied. - Expecting TRIM to change appearance of the original. The source cell is never edited. If you copy the helper column, you copy formulas that depend on column A. Deleting column A then produces
#REF!errors. Convert to values first. - Concluding TRIM failed. Check for code 160 before assuming the formula is wrong.
- Forgetting that blank-looking cells are not blank. A cell containing only spaces is not empty.
ISBLANKreturnsFALSEfor it, butTRIMreturns an empty string, so=TRIM(A2)=""returnsTRUE.
The last point is useful for finding rows that look empty:
=IF(TRIM(A2)="","Blank","Has text")Summary of behavior
| Situation | TRIM result |
|---|---|
| Leading or trailing spaces (code 32) | Removed |
| Several spaces between words | Reduced to one |
| Non-breaking space (code 160) | Unchanged |
| Tab or line feed | Unchanged |
| Number input | Text output |
| Cell with only spaces | Empty string |
Exercise
Cell A2 contains the text " Ana Souza ". You enter =LEN(A2)-LEN(TRIM(A2)) in B2 and get 6. A different cell, A3, looks padded in the same way, but the same formula returns 0. Name two likely explanations for A3.
Show answer
In A2, TRIM removes 2 leading, 2 trailing, and 3 of the 4 inner spaces, so 2 + 2 + 3 = 7 would be expected for the text as written here. A result of 6 means the actual padding differs from what you typed, which is why the formula result is the one to trust. For A3, a result of 0 means TRIM found nothing it can remove. Either the cell has no extra spaces, or the padding uses characters other than code 32, most likely non-breaking spaces (code 160) or tabs. Check with =UNICODE(RIGHT(A3,1)) and a similar test on the first character.
Exercise
Write a formula for B2 that returns the text in A2 with outer spaces removed and inner runs reduced to one space, and returns the word "Missing" when A2 holds only spaces or is empty.
Show answer
=IF(TRIM(A2)="","Missing",TRIM(A2))