Text function
TRIM function in Google Sheets and Excel
TRIM removes spaces from the start and end of text and shrinks runs of spaces between words to one.
Checked against Microsoft's and Google's documentation on · Example calculated on a live sheet
- Excel 2016
- Excel 2019
- Excel 2021
- Excel 2024
- Microsoft 365
- Google Sheets
Syntax
TRIM(text)Arguments in [brackets] are optional. Google Sheets writes it TRIM(text).
Example
=TRIM(A2) reads: Take the name in A2 (" mara okafor ") and return it without the spaces before and after.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Name | Member code | City | Trimmed | |
| 2 | mara okafor | mara.okafor@example.com | GB-2041 | London | mara okafor |
| 3 | JONAS LIND | jonas.lind@example.org | SE-1187 | Malmö | |
| 4 | priya raman | priya.raman@example.net | IN-3302 | Pune | |
| 5 | Tomás Ruiz | tomas.ruiz@example.com | ES-0419 | Seville | |
| 6 | elif demir | elif.demir@example.org | TR-2750 | Izmir | |
| 7 | Kenji MORI | kenji.mori@example.net | JP-1093 | Osaka | |
| 8 | sofia rossi | sofia.rossi@example.com | IT-4468 | Turin | |
| 9 | Leo Brandt | leo.brandt@example.org | DE-3321 | Leipzig |
E2 shows mara okafor: the spaces before and after are gone (the capitals come next).
How the spreadsheet calculates it
Removes extra spaces from A2: at the start, at the end, and doubled spaces between words.
- 1. TRIM: Removes extra spaces from
A2: at the start, at the end, and doubled spaces between words.text=`A2`: the cell to clean
More TRIM examples
=PROPER(TRIM(A2))A name without stray spaces and with capitals: Mara Okafor.=LEN(A2)-LEN(TRIM(A2))How many extra spaces A2 had.=TRIM(SUBSTITUTE(A2,CHAR(160)," "))Removes web-page non-breaking spaces as well.=IF(TRIM(A2)=A2,"Clean","Has extra spaces")Flags the cells that need cleaning.
Paste any of these into the formula explainer to see it step by step.
Tips
- Trim lookup values and keys before blaming the lookup for #N/A.
- Combine it with PROPER for names:
PROPER(TRIM(A2)). - After cleaning, copy and paste as values, then delete the helper column.
What TRIM is used for
Cleaning names, emails and IDs pasted from forms, exports and web pages, so lookups match, duplicates are found and sorted lists line up. It's usually the first step before PROPER, lookups or removing duplicates.
Arguments
| Argument | Required | What it is |
|---|---|---|
| text | Yes | The text or cell to clean, such as A2. TRIM returns a cleaned copy and leaves the original alone. |
Common mistakes
Expecting TRIM to fix capitals: it only touches spaces.
Expecting TRIM to remove non-breaking spaces from web pages: it can't (that comes in a later lesson).
Pointing TRIM at its own cell to clean a column in place: that's a circular reference. Clean into a helper column, then paste the results as values if you need to replace the originals.
Looking up or comparing the untrimmed text: a key with a trailing space doesn't match. Trim inside the formula, as in
VLOOKUP(TRIM(M1),…), or clean the column first.
In Google Sheets
Google Sheets has TRIM(text) with the same single argument. Google's function list summarises it as removing leading and trailing spaces; we haven't verified that Sheets also shrinks runs of spaces between words the way Microsoft documents for Excel, so test that case in your file.
Errors you may see with TRIM
- #N/A: Not TRIM's own error, but the reason to use it: a lookup value with a stray space shows #N/A even though the value is in the list. How to fix it.
Questions
Why doesn't TRIM remove all the spaces?
It keeps single spaces between words on purpose, and it can't see the non-breaking space (character 160) that web pages use. Turn those into normal spaces first: TRIM(SUBSTITUTE(A2,CHAR(160)," ")).
How do I remove every space, even between words?
Replace spaces with nothing: =SUBSTITUTE(A2," ",""). Use it for codes and phone numbers, not for names.
How do I count the extra spaces?
Compare the lengths: =LEN(A2)-LEN(TRIM(A2)). For " mara okafor " that's 3.
What's the difference between TRIM and CLEAN?
TRIM removes spaces; CLEAN removes non-printing characters such as line breaks. Text pasted from other systems often needs both: TRIM(CLEAN(A2)).
Sources
Official documentation the facts on this page were checked against.