Example 1
Remove extra spaces from a name
C2 =TRIM(A2)
Result: Anya Kowalski
Leading and trailing spaces go, and the three spaces between the names become one.
C2 =TRIM(A2) returns Anya Kowalski
Removes spaces from the start and end of text, and reduces runs of spaces between words to a single space.
Syntax
=TRIM()
All versions of Excel.
Try it
Edit the formula in C2 or change the inputs. The sheet recalculates as you type.
C2 =TRIM(A2) returns Anya Kowalski
TRIM removes extra spaces from text. It deletes spaces at the start and end, and reduces any run of spaces between words to one. Data pasted from other systems, exported from the web or typed by hand often has such spaces, and they cause lookups, matches and comparisons to fail while the text looks perfectly fine.
=TRIM(" Anya Kowalski ") returns Anya Kowalski. The two leading spaces and two trailing spaces are removed, and the three spaces in the middle become one.
That is all it does. It doesn't change the case of letters, and it doesn't touch other whitespace characters.
A trailing space is invisible but real. If a product code is "FX-1010 " in one list and "FX-1010" in another, XLOOKUP and VLOOKUP report #N/A. Compare the two cells to confirm: =TRIM(A2)=B2 returns TRUE when the only difference was extra spaces.
Fix the data by cleaning a copy, not by changing the formulas that depend on it. Put =TRIM(A2) in a helper column and copy it down. When you are sure the result is right, copy that column and paste it as values over the original.
You can also trim inside a lookup: =XLOOKUP(TRIM(A2),D2:D9,E2:E9). That fixes the lookup value, but not extra spaces in the list being searched.
TRIM removes the standard space, character 32. It leaves two kinds of look-alikes:
=TRIM(SUBSTITUTE(A2,UNICHAR(160)," ")).A full clean for data of unknown origin is =TRIM(CLEAN(SUBSTITUTE(A2,UNICHAR(160)," "))).
TRIM returns text. If the cell holds the text " 1250 ", the result is the text "1250". It still won't add up in a SUM. Convert it with VALUE: =VALUE(TRIM(A2)).
Applied to a real number, TRIM turns it into text. Avoid trimming cells that were already numbers.
=LEN(A2)-LEN(TRIM(A2)) counts the spaces that TRIM removes. A result above 0 means the cell has leading, trailing or repeated spaces. Use it as a quick audit across a column before you decide to clean.
TRIM is often the outer layer of a join. =TRIM(A2&" "&B2&" "&C2) joins three cells with spaces and removes the extra space left when B2, the middle name, is empty. TEXTJOIN with ignore_empty set to TRUE does the same job more neatly.
A2. TRIM removes ordinary spaces (character 32) only, not non-breaking spaces or line breaks.Each example is a live sheet. Change the inputs or the formula and watch the result.
Example 1
C2 =TRIM(A2)
Result: Anya Kowalski
Leading and trailing spaces go, and the three spaces between the names become one.
C2 =TRIM(A2) returns Anya Kowalski
Example 2
C2 =TRIM(A2)=B2
Result: TRUE
A2 ends in a space, so A2=B2 is FALSE. After TRIM, the two codes are equal.
C2 =TRIM(A2)=B2 returns TRUE
Example 3
C2 =TRIM(SUBSTITUTE(A2,UNICHAR(160)," "))
Result: Floor cleaner
The gap between the words is two non-breaking spaces. TRIM alone would leave them. SUBSTITUTE converts them to ordinary spaces first, and TRIM reduces the pair to one.
C2 =TRIM(SUBSTITUTE(A2,UNICHAR(160)," ")) returns Floor cleaner
Example 4
C2 =LEN(A2)-LEN(TRIM(A2))
Result: 6
The cell has 19 characters. TRIM leaves 13. The difference is 2 leading, 2 extra in the middle and 2 trailing.
C2 =LEN(A2)-LEN(TRIM(A2)) returns 6
Solve it in the sheet. Every answer is checked, with hints if you need them.
Task 1 of 2
In B2, return the name in A2 with no leading or trailing spaces and single spaces between words. Fill it down to B5.
Answer in B2, then it fills down to B5.
SUBSTITUTE(A2,UNICHAR(160)," ") first." 1250 " is the text "1250", which SUM ignores. Wrap it in VALUE for a number.Use `=TRIM(A2)`. It removes leading and trailing spaces and reduces multiple spaces between words to one. Copy the results and paste them as values to replace the original data.
The space is probably a non-breaking space, character 160, common in text copied from web pages. Use `=TRIM(SUBSTITUTE(A2,UNICHAR(160)," "))`. A line break or tab needs CLEAN.
A hidden trailing or leading space is the usual cause. Compare `=LEN(A2)` with the length you expect, or test `=TRIM(A2)=B2`.
No. TRIM only deals with spaces. Combine it with PROPER, UPPER or LOWER for case, for example `=PROPER(TRIM(A2))`.
TRIM removes extra spaces. CLEAN removes non-printing characters such as line breaks and tabs. For messy data, use both: `=TRIM(CLEAN(A2))`.
TRIM is taught in Logic and text, lesson 7: TRIM and CLEAN. Each lesson is a short, graded exercise like the one above.