Excel functions · Text
TRIM function in Excel
TRIM removes extra spaces from text: every space at the start and end, and all but one space between words. It's the first fix when a lookup fails or a count is off because of spaces you can't see, though it doesn't remove the non-breaking spaces that come from web pages.
Last reviewed
Syntax
=TRIM(text)- textrequired
- The text, or cell, to clean up.
- TRIM removes the ordinary space character only. Non-breaking spaces (CHAR(160)) need SUBSTITUTE first.
- CLEAN removes non-printing characters such as line breaks from other systems; TRIM(CLEAN(A2)) does both.
- TRIM returns new text; to replace the original, copy the results and paste as values.
Which versions have TRIM
Every current Excel version: Microsoft 365, Excel for the web, Excel 2024, 2021, 2019 and 2016, on Windows and Mac.
| Version | Has it |
|---|---|
| Microsoft 365 (Windows and Mac) | Yes |
| Excel for the web | Yes |
| Excel 2024 | Yes |
| Excel 2021 | Yes |
| Excel 2019 | Yes |
| Excel 2016 | Yes |
Google Sheets
Google Sheets has TRIM, which also removes leading, trailing and repeated spaces.
Examples
The examples use this sample sheet.
| Row | A |
|---|---|
| 1 | Company |
| 2 | " Acme Corp " |
| 3 | "Acme Corp " |
Quotes show where the spaces are; they aren't in the cells. A2 has 2 leading spaces, 3 between the words and 1 trailing. A3 ends with a non-breaking space pasted from a web page.
1.Remove extra spaces
=TRIM(A2)Result: Acme Corp
Leading and trailing spaces go, and the three spaces between words become one.
2.Count the spaces removed
=LEN(A2)-LEN(TRIM(A2))Result: 5
A2 is 14 characters long and the trimmed text is 9.
3.Non-breaking spaces
=TRIM(SUBSTITUTE(A3, CHAR(160), " "))Result: Acme Corp
TRIM alone leaves the non-breaking space; SUBSTITUTE turns it into a normal space first.
Common errors and fixes
| Error | Why it happens | Fix |
|---|---|---|
| Spaces still there | They're non-breaking spaces (CHAR(160)), common in data copied from web pages. | =TRIM(SUBSTITUTE(A2, CHAR(160), " ")). |
| #N/A in a lookup | The key or the table has extra spaces, so values that look the same don't match. | Clean both sides with TRIM, or look up TRIM(value). |
Do it with GPTExcel
Skip writing TRIM by hand
Upload your Excel or CSV file to Chat and ask in plain English. GPTExcel writes the formula for your file and your version of Excel or Google Sheets.
Clean the spaces in my customer names and tell me which rows changed.
Done. I used TRIM on your sheet and checked the results against your data.
- Written for your platform and version
- Checked against your actual data
- Newest functions looked up in official docs
TRIM questions
- Why doesn't TRIM remove all spaces?
- TRIM removes only the standard space character and keeps single spaces between words. Non-breaking spaces from web pages are a different character (CHAR(160)); replace them with SUBSTITUTE first.
- How do I remove all spaces, including between words?
- Use SUBSTITUTE instead of TRIM: =SUBSTITUTE(A2, " ", "") removes every space.
Skip the formula hunt
Upload a spreadsheet and ask in plain English. Free plan, no credit card.
Start free