LEN and TRIM in Excel: Clean & Count Text Right

Learn how to clean and measure text data.

Three hours. I'd been staring at a VLOOKUP that refused to return anything except #N/A, checking the range, checking the syntax, checking whether I'd lost my mind, and the answer turned out to be a trailing space in the lookup column. A single invisible character. "I could have built a Power Query solution from scratch in less time," I told my team. That was year four of twelve, and I still think about it every time I open a CSV export. The LEN function in Excel and the TRIM function in Excel are what I reach for now before I touch anything else, because leading and trailing spaces, along with other invisible characters, are the single most common reason formulas silently fail.

Most tutorials cover these two functions separately, like they're unrelated tools. They're not. Used together, they let you remove extra spaces and accurately count characters from messy imported data in a single formula. This guide treats them as a deliberate pair. You'll need any version of Microsoft Excel, including Microsoft 365, or Google Sheets, and a column of real, imperfect text. If you're newer to text functions generally, the Excel Formulas and Functions for Beginners overview is worth a quick read first.

Step 1: Use TRIM to Strip Leading, Trailing, and Extra Internal Spaces Before You Count Anything

Before LEN gives you a useful number, the text needs to be clean. TRIM handles the basics: it removes any spaces at the start of a cell, any spaces at the end, and collapses multiple internal spaces down to one. If your Order Reference Number column came from a CRM export, there are almost certainly extra spaces in it. There just are.

Write the formula in an empty column next to your data:

  1. Click the first empty cell next to your text column (say, B2 if your data is in column A).
  2. Type =TRIM(A2) and press Enter.
  3. Copy the formula down to cover all your rows.
  4. Compare column B to column A. If anything looks different, those spaces were real.

I used to skip this step and wonder why my character counts were off by one or two. The fix was always here.

What TRIM Actually Removes (and the One Space It Misses)

TRIM was built to handle the standard ASCII space character, decimal value 32. That covers most spreadsheet problems. But text copied from a website, or pulled from certain CRM or ERP exports, often contains non-breaking spaces: a different character entirely, decimal value 160, sometimes written as   in HTML. TRIM doesn't touch those. They're invisible, they're not space character 32, and they will absolutely break a VLOOKUP or SUMIFS just as efficiently as a regular trailing space.

If TRIM cleans your column but the formula still fails, non-breaking spaces are the most likely culprit. The fix uses SUBSTITUTE and CHAR(160), covered in Step 2 as part of the full nested formula.

There's also a separate category of problem: non-printable characters such as line breaks or carriage returns embedded in imported data. TRIM doesn't remove those either. CLEAN does. If you're dealing with truly nightmarish source data, =TRIM(CLEAN(A2)) is the combination worth knowing. During my consulting years between 2016 and 2019, I ran that formula more times than I'd like to count.


Step 2: Nest LEN Inside TRIM to Count Characters Without Counting Stray Spaces

Once your text is clean, you're ready to measure it. The core formula is =LEN(TRIM(A2)). Excel evaluates the innermost function first, so TRIM runs on A2 before LEN sees anything. LEN then counts the characters in whatever TRIM returns. That nesting order is the whole point: you're counting the text, not the padding around it.

If =LEN(A2) returns 8 and you only typed 7 characters, that extra count is a trailing space. Run =LEN(TRIM(A2)) to confirm it.

For a quick audit across a column, run =LEN(A2)-LEN(TRIM(A2)). Any cell that returns a number greater than zero has extra spaces. It's a fast way to flag problems before deciding how to fix them.

Fix Non-Breaking Spaces with SUBSTITUTE Before LEN Runs

If your data came from a web paste or a system export, the full version of this formula is:

=LEN(TRIM(SUBSTITUTE(A2, CHAR(160), " ")))

Reading from the inside out: SUBSTITUTE replaces every non-breaking space (CHAR(160)) with a regular space character. TRIM then removes all the extra spaces, including those newly converted ones. LEN counts what's left. Each argument has a job, and they run in sequence. Don't just copy this formula. Understanding why SUBSTITUTE comes first matters when something breaks and you need to debug it.

This is the formula most tutorials skip. The official Microsoft documentation on TRIM mentions non-breaking spaces only briefly. In practice, if your data has ever touched a web browser, assume CHAR(160) is in there somewhere.


Common Mistakes With LEN and TRIM in Excel (and How to Catch Them)

These are the three problems I see most often, including from people who've been using Excel for years.

Applying TRIM to a number instead of text. If A2 contains a numeric value (not a number stored as text, but an actual number), TRIM returns a #VALUE! error because it only operates on text strings. The fix is wrapping the value in TEXT() first, or confirming your column is genuinely text before applying TRIM. A number stored as text will often show an unexpected character count under LEN, which is a useful diagnostic in its own right.

Forgetting that LEN counts every character. LEN counts punctuation, hyphens, and any spaces TRIM didn't remove (like the CHAR(160) from Step 2). If your count seems off by one and TRIM didn't help, run the full SUBSTITUTE formula.

Running LEN on the wrong cell. This is a workflow error, not a formula error: copy-pasting the TRIM result as a value, then running LEN on the original cell instead of the cleaned one. This gives you a character count from dirty data, which defeats the entire purpose. If you paste TRIM results as values, make sure LEN references the pasted column, not the source.

Errors in spreadsheets aren't embarrassing accidents. They're the natural consequence of working without a validation layer. LEN and TRIM are part of that layer, but they're still a workaround. The permanent fix is enforcing clean data before it enters Excel, at the Power Query stage, so the trailing spaces never arrive in the first place. If you want to understand how that connects to formula structure more broadly, the Excel for Beginners starter guide covers that foundation well, and understanding cell references in Excel will help you apply these formulas correctly across larger datasets.

TRIM cleans the symptom. Power Query fixes the source.

Frequently Asked Questions

How do I use LEN and TRIM together in Excel?

Nest TRIM inside LEN like this: =LEN(TRIM(A2)). Excel runs TRIM first to remove extra spaces, then LEN counts the characters in the cleaned result. This gives you an accurate character count that excludes leading, trailing, and extra internal spaces.

Why does TRIM not remove all spaces in Excel?

TRIM only removes the standard ASCII space character (decimal value 32). Non-breaking spaces (decimal value 160), which are common in text copied from websites or exported from CRMs, are a different character that TRIM ignores entirely. Use =TRIM(SUBSTITUTE(A2, CHAR(160), " ")) to convert non-breaking spaces to regular ones before TRIM runs.

Why is my LEN TRIM formula returning the wrong result?

The most common cause is non-breaking spaces that TRIM didn't remove. Check by comparing =LEN(A2) to =LEN(TRIM(A2)). If they're equal but your count still seems wrong, the cell may contain non-printable characters; try =LEN(TRIM(CLEAN(A2))) instead. Also confirm you're running LEN on the cleaned cell, not the original source data.