TRIM Function: Remove Extra Spaces & Clean Dirty Data

TRIM Formula in Excel Blogpost Feature Image
Learn how to use the TRIM function in Excel to remove leading, trailing, and extra spaces. Covers non-breaking spaces, TRIM+CLEAN, fixing broken lookups, and word counting.

Hidden spaces are one of the most common causes of broken formulas, failed lookups, and incorrect sort orders. They arrive silently — through copied text, web imports, database exports, and manual data entry. The TRIM function solves this in one step. It removes all leading spaces, all trailing spaces, and any sequence of more than one space between words. As a result, every cell holds clean, consistent text that other formulas can work with reliably.

Availability: The Formula TRIM works in every Excel version from Excel 2000 onwards, including Microsoft 365, Excel 2024, 2021, 2019, and 2016. It also works in Google Sheets.

What Is the TRIM Syntax?

TRIM takes a single argument. The syntax is as simple as it gets.

=TRIM(text)
ArgumentRequired?What it does
text Required The string to clean. Use a cell reference for real data, or type text directly in quotes for a quick test. TRIM returns the cleaned text as a new value — the original cell is not changed.

This function removes the ASCII space character (character code 32). Specifically, it deletes all leading spaces before the first word, all trailing spaces after the last word, and any run of two or more consecutive spaces between words — replacing each run with a single space. Critically, it leaves exactly one space between words intact.

Furthermore, it does NOT remove non-breaking spaces: Text copied from websites often contains non-breaking spaces (character code 160). These look identical to regular spaces but TRIM cannot see them. Example 4 shows the fix — a one-formula solution using SUBSTITUTE and CHAR(160).

How Does TRIM Work on Each Type of Space?

The character visualiser below shows exactly which spaces TRIM removes (red) and which it preserves (green). Understanding this distinction prevents surprises when working with multi-word strings.

Before TRIM — " Sales Report "
·
lead
·
lead
S
a
l
e
s
·
extra
·
extra
R
e
p
o
r
t
·
trail
·
trail
After TRIM — "Sales Report"
S
a
l
e
s
·
kept
R
e
p
o
r
t

■ Red = removed    ■ Green = kept    ■ Grey = one space preserved between words

Example 1: Clean Names and Labels from Imported Data

Leading and trailing spaces cause immediate problems. A VLOOKUP looking for "London" will not match " London" because they are technically different strings. Similarly, a SUMIF on "Approved" misses " Approved" entirely. TRIM fixes these mismatches before they cause errors downstream.

1
Remove leading, trailing, and double spaces from imported names
Before — spaces cause lookup failures

··Sarah Connor

James Kirk··

·Ellen Ripley·

After TRIM — clean and consistent

Sarah Connor

James Kirk

Ellen Ripley

Basic — remove all extra spaces from cell A2: =TRIM(A2) Count characters before and after — shows how many spaces were removed: LEN(A2) → original length including spaces LEN(=TRIM(A2)) → cleaned length LEN(A2) - LEN(=TRIM(A2)) → number of spaces removed Check if a cell needs trimming — TRUE means spaces exist: A2 <> =TRIM(A2)
Use the inequality check A2 <> TRIM(A2) in a helper column to flag every cell that contains hidden spaces. This is a fast audit step before cleaning a large dataset.

Example 2: Fix Broken Lookups Caused by Spaces

Invisible spaces break VLOOKUP, XLOOKUP, MATCH, and SUMIF without any error message. The formula simply returns #N/A or zero because the lookup value and the table value look the same to the eye but differ in their actual character content. Wrapping the lookup value in TRIM fixes this immediately.

2
Fix #N/A errors in XLOOKUP and SUMIF caused by hidden spaces
A — Lookup key
Spaces present?
Without TRIM
With TRIM
" London " (leading+trail)
Yes
#N/A
£4,200
"London" (clean)
No
£4,200
£4,200
XLOOKUP — wrap the lookup value in TRIM to clean it before searching: =XLOOKUP(=TRIM(A2), D:D, E:E, "Not found") SUMIF — clean the criteria cell before using it as the condition: =SUMIF(A:A, =TRIM(F2), B:B) MATCH — TRIM the lookup array too if the table itself has spaces: MATCH(=TRIM(A2), =TRIM(D2:D100), 0) IFERROR wrapper handles residual mismatches gracefully: =IFERROR(XLOOKUP(=TRIM(A2), D:D, E:E), "Check source data")
Clean the table too, not just the lookup value: If spaces exist in both the lookup key and the lookup table, wrap TRIM around the table array as well: =MATCH(TRIM(A2), TRIM(D2:D100), 0). This ensures a clean-to-clean comparison.

Example 3: Combine TRIM with CLEAN for Fully Dirty Data

Data imported from databases, APIs, or legacy systems often contains both extra spaces and non-printable characters — line breaks, carriage returns, or control characters from other systems. TRIM removes the spaces. CLEAN removes the first 32 non-printable ASCII characters. Combining both functions produces the most thorough basic clean possible.

3
Remove spaces AND non-printable characters from imported data
CLEAN removes non-printable characters (codes 0–31). TRIM removes the extra spaces that CLEAN may leave behind. Always put TRIM outside CLEAN — TRIM processes the CLEAN result: =TRIM(CLEAN(A2)) What each function removes: CLEAN → line breaks, carriage returns, tab characters, null chars (codes 0–31) TRIM → leading spaces, trailing spaces, double spaces between words Verify the result is truly clean — compare lengths: If LEN(TRIM(CLEAN(A2))) still differs from LEN(TRIM(A2)), non-printable characters were found and removed by CLEAN. LEN(=TRIM(A2)) <> LEN(=TRIM(CLEAN(A2))) Returns TRUE if CLEAN actually removed something
TRIM + CLEAN does not remove everything: CLEAN only handles the first 32 ASCII control characters. Some extended Unicode characters and certain printer control codes fall outside this range. For unusual characters from SAP or Oracle exports, you may need SUBSTITUTE with CHAR() for specific codes. Example 4 covers the most common missed case — the non-breaking space.

Example 4: Fix Non-Breaking Spaces from Web and CMS Data

Non-breaking spaces are invisible to the eye and immune to TRIM. They arrive when you paste text from a web page, a CMS, or a PDF. Their character code is 160, not 32 — so TRIM simply ignores them. Fortunately, SUBSTITUTE with CHAR(160) converts them to regular spaces first, and then TRIM cleans up the result.

4
Remove non-breaking spaces that TRIM alone cannot clean
A — Pasted text
=TRIM(A2)
Still spaces?
Full fix
" London " (NBSP)
" London " ← fails
Yes
London ✓
TRIM alone does NOT remove non-breaking spaces (CHAR 160): =TRIM(A2) → still contains invisible NBSP characters Fix — replace CHAR(160) with a normal space CHAR(32), then TRIM: SUBSTITUTE converts each NBSP to a regular space. TRIM then removes the extra regular spaces. =TRIM(SUBSTITUTE(A2, CHAR(160), " ")) Full all-in-one clean — handles NBSP, non-printable chars, and extra spaces: =TRIM(CLEAN(SUBSTITUTE(A2, CHAR(160), " "))) Detect NBSP without cleaning — TRUE means NBSP is present: LEN shrinks only if SUBSTITUTE found and removed CHAR(160). LEN(A2) <> LEN(SUBSTITUTE(A2, CHAR(160), ""))
The formula =TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))) is the gold standard for cleaning web-pasted text. It handles non-breaking spaces, non-printable control characters, and all regular extra spaces in a single pass.

Example 5: Use TRIM to Count Words

Combining TRIM and LEN gives you a reliable word count formula. The approach counts the spaces between words and adds one. TRIM is essential here because extra spaces would inflate the count and produce wrong results. This technique is useful for validating input length and checking data quality.

5
Count words in a cell accurately — TRIM ensures extra spaces do not inflate the count
A — Text
LEN without TRIM
Word count
Over 5 words?
"Sales Report Q1"
18 (with extra spaces)
3
No
" Q1 FY2026 UK Budget Review "
35 (inflated)
5
No (exactly 5)
Word count formula — count spaces in trimmed string, then add 1. Each space in a trimmed string represents one word boundary. =LEN(=TRIM(A2)) - LEN(SUBSTITUTE(=TRIM(A2), " ", "")) + 1 Explanation: LEN(TRIM(A2)) → total characters after trimming LEN(SUBSTITUTE(TRIM(A2)," ","")) → characters with spaces removed Difference = number of spaces in the trimmed string = words - 1 Add 1 → word count Returns 0 for empty cells — wrap in IF for safety: =IF(=TRIM(A2)="", 0, LEN(=TRIM(A2))-LEN(SUBSTITUTE(=TRIM(A2)," ",""))+1)

Example 6: Paste Cleaned Values Back — The Helper Column Workflow

TRIM returns cleaned text in a new cell. It does not overwrite the original. To permanently replace the dirty data with clean values, you need to copy the TRIM results and paste as values. This standard workflow takes four steps and works in any Excel version.

How to Replace Original Data with Cleaned Values

First, add a helper column next to your data and enter =TRIM(A2) in the first row. Then copy the formula down to cover all rows. Next, select the helper column results, copy them (Ctrl+C), click the first cell of the original column, and choose Paste Special → Values (Alt+E+S+V, then Enter). Finally, delete the helper column. The original column now holds permanently clean data with no formula dependency.

6
Apply TRIM across a full column and convert results to static values
Step 1 — enter TRIM formula in a helper column next to column A: =TRIM(A2) copy down to cover all rows Step 2 — for the strongest clean, use the full combination: This handles non-breaking spaces AND non-printable characters in one formula. =TRIM(CLEAN(SUBSTITUTE(A2, CHAR(160), " "))) Step 3 — select cleaned cells, Ctrl+C, click column A row 1, then: Home → Paste → Paste Special → Values (or Alt+E+S+V+Enter) Alternative — remove all spaces including between words with SUBSTITUTE: Use ONLY when you want no spaces at all (e.g. cleaning phone numbers). SUBSTITUTE(A2, " ", "") → removes every single space
Keyboard shortcut for Paste Special Values: After copying, press Alt+E+S+V and then Enter. Alternatively, press Ctrl+Shift+V in some configurations. Pasting as values disconnects the formula — the cleaned text remains even after you delete the helper column.

How to Fix Common TRIM Problems

TRIM does not remove all spaces

If spaces remain after TRIM, the cell likely contains non-breaking spaces (CHAR 160). TRIM only removes CHAR 32. Apply the full fix: =TRIM(SUBSTITUTE(A2,CHAR(160)," ")). To confirm the issue, check whether LEN(A2) <> LEN(SUBSTITUTE(A2,CHAR(160),"")) returns TRUE.

TRIM returns a number instead of text

TRIM always returns a text string. If your source cell contains a number with leading spaces, TRIM returns the number formatted as text. As a result, arithmetic formulas may error. Wrap TRIM in VALUE to convert back: =VALUE(TRIM(A2)). Alternatively, multiply the TRIM result by 1.

Lookup still fails after TRIM

Hidden characters other than spaces may still be present. Try the full three-function formula: =TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))). Additionally, check whether the lookup table has spaces too — TRIM both sides of the comparison, not just the key.

TRIM vs Find and Replace: Ctrl+H with two spaces in Find and one in Replace only removes double spaces — not leading spaces, trailing spaces, or triple spaces. TRIM is therefore much more thorough and reliable. Use Find and Replace only for a quick one-off fix in a small range where formula overhead is not desirable.

Frequently Asked Questions About TRIM

  • What does the TRIM function do in Excel?+
    TRIM removes all leading spaces from the start of a text string, all trailing spaces from the end, and any sequence of multiple spaces between words — leaving exactly one space between each word. It targets the standard ASCII space character (code 32). TRIM does not remove non-breaking spaces (code 160), which arrive when text is pasted from web pages or other applications.
  • Why is TRIM not removing all the spaces?+
    The remaining spaces are almost certainly non-breaking spaces (CHAR 160). These arrive from web pages and CMS systems and look identical to regular spaces but have a different character code. TRIM cannot remove them. To fix this, use: =TRIM(SUBSTITUTE(A2,CHAR(160)," ")). For the most thorough clean, combine with CLEAN as well: =TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))).
  • Does TRIM remove spaces between words?+
    Partially. TRIM reduces multiple consecutive spaces between words to a single space. However, it always keeps one space between words — it does not remove single spaces. If you want to remove every space including between words (for example, when cleaning phone numbers), use SUBSTITUTE instead: =SUBSTITUTE(A2," ",""). That replaces every space character with nothing.

More TRIM Questions

  • What is the difference between TRIM and CLEAN?+
    TRIM removes extra space characters (code 32) — leading, trailing, and duplicated. CLEAN removes the first 32 non-printable ASCII characters (codes 0 through 31), such as line breaks, carriage returns, and tab characters. Use both together for data from external systems: =TRIM(CLEAN(A2)). Wrap TRIM around CLEAN so it cleans up any spaces that CLEAN may leave behind.
  • How do I apply TRIM to a whole column permanently?+
    Use a helper column: enter =TRIM(A2) in an adjacent column and copy it down to cover all rows. Then select the helper column, press Ctrl+C to copy, click the first cell of the original column, and use Paste Special → Values (Alt+E+S+V, then Enter). This replaces the original dirty data with clean static text. Delete the helper column afterward.
  • Which Excel versions support TRIM?+
    TRIM is available in every Excel version from Excel 2000 onwards, including Microsoft 365, Excel 2024, 2021, 2019, 2016, and all earlier versions back to Excel 97. It also works in Google Sheets and Excel for the web. No special version or add-in is required.