TRIM function
Removes spaces at the start and end of text and reduces any run of spaces between words to a single space.
Available in All versions of Excel, including Microsoft 365, Excel for Mac and Excel for the web
In every Excel versionWorks in Google Sheets
Syntax
TRIM(text)| Argument | What it means | |
|---|---|---|
text | Required | The text or cell to clean. |
Returns: The text with extra spaces removed.
How TRIM works
Extra spaces are the most common reason why "identical" values do not match. Data typed by people, exported from other systems or pasted from emails often has a space at the end (Mouse ) or doubled spaces in the middle (Ana Silva). They are invisible, but VLOOKUP, XLOOKUP, COUNTIF and duplicate checks treat the values as different. TRIM fixes this: it removes all leading and trailing spaces and squeezes every run of spaces between words down to one.
TRIM only removes the normal space character (code 32). Web pages often use the non-breaking space (code 160), which TRIM ignores; replace it first with SUBSTITUTE(A2,CHAR(160)," "). Line breaks and other control characters need CLEAN. Together they form the standard cleaning formula =TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))).
TRIM returns a new value; to clean the original column, copy the TRIM results and Paste Values over it. In Microsoft 365 you can trim a whole range in one go: =TRIM(A2:A500) spills the cleaned list.
Examples
1Clean up a name
| A | B | |
|---|---|---|
| 1 | Name (imported) | |
| 2 | Ana Silva | Ana Silva |
Result: Ana Silva
Leading and trailing spaces are removed and the three spaces between the names become one.
2Make a lookup work
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Product | Price | Lookup | Price found | |
| 2 | Keyboard | 25 | Mouse | 12 | |
| 3 | Mouse | 12 | |||
| 4 | Monitor | 180 |
Result: 12
D2 contains Mouse with a trailing space, so a plain VLOOKUP returns #N/A. TRIM removes the space and the match succeeds.
3Non-breaking spaces from a web page
| A | B | |
|---|---|---|
| 1 | Product (copied from web) | |
| 2 | Laptop Stand | Laptop Stand |
Result: Laptop Stand
On its own TRIM would leave the text unchanged, because the spaces are code 160. SUBSTITUTE turns them into normal spaces first.
Tips
- Check whether a cell has extra spaces with
=LEN(A2)<>LEN(TRIM(A2)). - Use TRIM inside lookups rather than on the lookup table when you cannot change the source data.
- Power Query's Transform > Format > Trim removes only leading and trailing spaces, not doubled inner spaces - TRIM in the sheet does both.
Questions
Why does TRIM not remove some spaces?
They are probably non-breaking spaces (code 160) from a web page. Use =TRIM(SUBSTITUTE(A2,CHAR(160)," ")).
Does TRIM remove single spaces between words?
No. It keeps one space between words and only removes extras plus any at the start and end.
How do I remove all spaces?
Use =SUBSTITUTE(A2," ",""), which deletes every space including those between words.
Google Sheets
TRIM also exists in Google Sheets and works the same way for everyday use, so the examples above work there too.
TRIM in other languages
In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.
| Language | Function name |
|---|---|
| Czech | PROČISTIT |
| Danish | FJERN.OVERFLØDIGE.BLANKE |
| Dutch | SPATIES.WISSEN |
| Finnish | POISTA.VÄLIT |
| French | SUPPRESPACE |
| German | GLÄTTEN |
| Hungarian | KIMETSZ |
| Italian | ANNULLA.SPAZI |
| Norwegian (Bokmål) | TRIMME |
| Polish | USUŃ.ZBĘDNE.ODSTĘPY |
| Portuguese (Brazil) | ARRUMAR |
| Portuguese (Portugal) | COMPACTAR |
| Russian | СЖПРОБЕЛЫ |
| Spanish | ESPACIOS |
| Swedish | RENSA |
| Turkish | KIRP |
Recipes that use TRIM
- How to combine first and last name in Excel
- How to remove duplicates in Excel
- How to remove line breaks in Excel
- How to remove spaces in Excel
- How to split first and last name in Excel