Excel Formula to Trim Non-Breaking Spaces Using SUBSTITUTE and CHAR

📅 Aug 16, 2026 📝 Sarah Miller

Cleaning imported financial data often leaves stubborn, invisible spaces that break your Excel formulas. When managing budget sheets, standard funding sources like federal grants or municipal allocations frequently export with these formatting anomalies. Resolving this issue grants immediate data integrity and flawless calculations.

However, as a crucial stipulation, the standard TRIM function only targets regular spaces (CHAR(32)), completely ignoring non-breaking web spaces (CHAR(160)). For instance, database records labeled "Grants " will fail to aggregate until cleaned. Below, we outline how to combine SUBSTITUTE and CHAR to permanently purge these hidden obstacles.

Excel Formula to Trim Non-Breaking Spaces Using SUBSTITUTE and CHAR

Introduction: The Mystery of the Stubborn Spaces in Excel

We have all been there: you import a dataset from a web browser, an ERP system like SAP, or an external cloud platform like Salesforce into Microsoft Excel. Visually, the data looks absolutely flawless. However, when you try to run a VLOOKUP, XLOOKUP, or MATCH formula, Excel returns the dreaded #N/A error.

Frustrated, you try to fix the issue using Excel's native TRIM function, expecting it to strip away all those annoying leading, trailing, and duplicate spaces. But nothing happens. The extra spaces remain, and your formulas continue to fail. You might even test if the cell is truly identical to your search key using a simple logical test like =A1=B1, only for Excel to stubbornly output FALSE.

What is going on here? The culprit is almost certainly the non-breaking space. In this comprehensive guide, you will learn exactly what non-breaking spaces are, why standard Excel tools fail to remove them, and how to build a robust formula using SUBSTITUTE and CHAR to clean your data once and for all.

What is a Non-Breaking Space (and Why Does TRIM Ignore It)?

To understand why the TRIM function fails us, we have to look at the underlying character codes that computers use to render text. In the ASCII character set:

  • Standard Space (ASCII 32): This is the space character created when you press the spacebar on your keyboard. In Excel formulas, it is represented as CHAR(32).
  • Non-Breaking Space (ASCII 160): Commonly used in HTML and web design as the   entity, this character prevents an automatic line break at its position. In Excel, it is represented as CHAR(160).

The standard Excel TRIM function is designed with a very specific, singular purpose: to remove leading and trailing standard spaces (ASCII 32) from a text string, and to condense consecutive multiple standard spaces down to a single space. The TRIM function has absolutely no effect on ASCII 160 characters. To Excel, a non-breaking space is not a "space" at all-it is treated as a distinct, printable alphabetical character. Because of this, standard cleanup methods simply ignore it.

The Solution: The TRIM, SUBSTITUTE, and CHAR Formula

To get rid of non-breaking spaces, we need to convert them into standard spaces first, and then let the TRIM function do its job. We can achieve this by nesting the SUBSTITUTE and CHAR functions inside a TRIM function.

Here is the universal formula to remove both standard and non-breaking spaces from cell A2:

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

How This Formula Works, Step-by-Step

This nested formula evaluates from the inside out. Let's break down exactly what Excel is doing under the hood:

  1. CHAR(160): This function returns the non-breaking space character. By using this instead of typing a space, we ensure we are targeting the exact hidden character causing the issue.
  2. SUBSTITUTE(A2, CHAR(160), " "): The SUBSTITUTE function looks inside cell A2, finds every instance of the non-breaking space (CHAR(160)), and replaces it with a standard keyboard space (" "). Note: It is crucial to replace it with a standard space rather than nothing ("") so that words originally separated by non-breaking spaces do not run together (e.g., transforming "New York" into "NewYork" instead of "New York").
  3. TRIM(...): Now that all the stubborn non-breaking spaces have been converted into standard spaces, the outer TRIM function can finally do its job. It instantly strips away any leading and trailing standard spaces, and shrinks any double spaces within the text down to a single space.

Practical Example: Cleaning Web-Scraped Data

Imagine you have imported a list of names and addresses from a company website. The data in cell A2 looks like this: "  John Doe  " (where   represents the hidden non-breaking space).

If you apply the standard formula =TRIM(A2), the result remains unchanged: " John Doe ".

However, if you apply our nested solution formula:

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

Excel executes the following transformation sequence:

  • Original: "[NBSP][NBSP]John[NBSP]Doe[NBSP][NBSP]"
  • After SUBSTITUTE: " John Doe " (all NBSPs successfully converted to regular spaces)
  • After TRIM: "John Doe" (clean, standardized text)

Taking It Further: The Ultimate "Super-Clean" Formula

Sometimes, data imported from legacy systems or external databases contains more than just standard and non-breaking spaces. You might also find non-printable characters, such as line breaks, carriage returns, or tabs. These characters correspond to ASCII codes 1 through 31.

To build a bulletproof formula that cleans almost any dirty text string, you can introduce the CLEAN function into the mix:

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

By nesting CLEAN inside the formula, Excel will remove all non-printable ASCII characters (like line breaks) in addition to swapping out and trimming the non-breaking spaces. This is the ultimate "Swiss Army Knife" formula for database administrators and data analysts working with raw text exports.

Special Case: Handling Mac Users

If you or your colleagues are working on Excel for Mac, you might occasionally run into characters that represent non-breaking spaces with different ASCII values. On certain older Mac installations, the non-breaking space can map to CHAR(202). If you suspect this is the case, you can expand your formula to handle both character sets:

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

Alternative Method: Using Find and Replace (Without Formulas)

If you prefer not to write formulas and want to clean your data directly in place, you can use Excel's native Find and Replace tool. However, because you cannot easily type a non-breaking space using your spacebar, you need to use a special keyboard trick.

  1. Select the range of cells you want to clean.
  2. Press Ctrl + H to open the Find and Replace dialog box.
  3. Click into the Find what input box.
  4. Hold down the Alt key on your keyboard, and using the numeric keypad (not the numbers above the QWERTY keys), type 0160. Release the Alt key. (Note: You won't see anything appear in the box, but the cursor will move slightly, indicating the character is there).
  5. Click into the Replace with input box and press your Spacebar once to insert a standard space.
  6. Click Replace All.

Once completed, Excel will replace every non-breaking space with a regular space. You can then run a standard TRIM formula over the column if necessary to clean up any remaining double spaces.

Automating Cleanups with Power Query

If you regularly import large datasets that require this type of cleaning, doing it manually or writing formulas every time can become tedious. This is where Power Query shines. Power Query automatically handles character cleaning during the data loading process.

In Power Query, you can clean columns by doing the following:

  1. Select the column you wish to clean.
  2. Right-click the column header and choose Replace Values.
  3. Under "Value to Find", you can enter the non-breaking space code, or simply copy a non-breaking space character directly from a source cell and paste it in.
  4. Under "Replace With", type a standard space.
  5. Click OK, and then use the Transform > Trim option on the ribbon to quickly remove leading and trailing spaces.

The beauty of this method is that your steps are recorded. The next time you refresh the query with new data, the non-breaking spaces will be cleaned automatically without any manual intervention.

Summary of Best Practices

Handling dirty data is an inevitable part of working with Microsoft Excel. When standard functions fail, understanding the underlying structure of characters is key. By combining TRIM, SUBSTITUTE, and CHAR(160), you can create a reliable, repeatable workflow to clean even the most stubborn web-scraped data, ensuring your lookups and data analyses run flawlessly every time.

Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.