formulastreak

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

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.

Workshop sign-ups · edit E2 and watch it recalculate
Click a cell (or use the arrow keys) to insert its address · Enter shows the resultE2 = mara okafor
ABCDE
1NameEmailMember codeCityTrimmed
2 mara okafor mara.okafor@example.comGB-2041Londonmara okafor
3JONAS LINDjonas.lind@example.orgSE-1187Malmö
4priya ramanpriya.raman@example.netIN-3302Pune
5Tomás Ruiz tomas.ruiz@example.comES-0419Seville
6elif demirelif.demir@example.orgTR-2750Izmir
7 Kenji MORIkenji.mori@example.netJP-1093Osaka
8sofia rossisofia.rossi@example.comIT-4468Turin
9Leo Brandtleo.brandt@example.orgDE-3321Leipzig

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. 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

ArgumentRequiredWhat it is
textYesThe 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.

Practise TRIM on a live sheet

Lesson 22, Tidy text: TRIM and PROPER (Unit 6, Text and dates), has 4 graded exercises, checked instantly, with steps and hints when you're stuck. Pro