Excel Formulas for Removing Multiple Spaces and Cleaning Data

📅 Feb 25, 2026 📝 Sarah Miller

Cleaning imported datasets riddled with irregular, multiple spaces is a frustrating bottleneck for data analysts. While enterprise IT funding sources typically prioritize large-scale system overhauls, everyday professionals require immediate, low-cost data prep solutions. Mastering native Excel functions grants immediate data integrity without the need for complex programming. The major stipulation, however, is that the basic TRIM function cannot remove stubborn non-breaking spaces commonly found in web exports. For instance, corporate finance teams routinely use nested TRIM and SUBSTITUTE formulas to sanitize ERP reports. Below, we explore the exact formulas to resolve these spacing anomalies.

Excel Formulas for Removing Multiple Spaces and Cleaning Data

Introduction to the Space Problem in Excel

Data cleaning is often the most time-consuming phase of any data analysis project. When importing data from external databases, web scraping, PDF converters, or CRM systems, one of the most common issues you will encounter is irregular spacing. Extra spaces-whether they are leading, trailing, or scattered in multiples between words-can disrupt your workflow and render your formulas useless.

For instance, if you are trying to perform a VLOOKUP, XLOOKUP, or MATCH, Excel treats "John Doe" and "John Doe" (with two spaces) as entirely different entities. This mismatch results in frustrating #N/A errors. In this comprehensive guide, we will explore the essential Excel formulas and techniques to replace multiple spaces and sanitize your dataset for seamless analysis.

The Standard Solution: The TRIM Function

The absolute foundation of space-cleaning in Excel is the TRIM function. Designed specifically for this purpose, TRIM is incredibly straightforward yet powerful.

How TRIM Works

The syntax for the function is simple:

=TRIM(text)

When you apply TRIM to a cell, Excel performs three distinct actions:

  • Removes all leading spaces (spaces at the very beginning of the text).
  • Removes all trailing spaces (spaces at the very end of the text).
  • Reduces any multiple consecutive spaces between words down to a single space.

Example of TRIM in Action

Imagine cell A2 contains the following messy text (with bracketed underscores representing spaces for visualization):
" Excel Formula Guide "

By entering the following formula in cell B2:

=TRIM(A2)

The output will be perfectly cleaned: "Excel Formula Guide".

The Hidden Obstacle: Non-Breaking Spaces (CHAR 160)

You might apply the TRIM function to your dataset only to find that several multiple spaces persist. Why does this happen? The culprit is usually the non-breaking space.

Standard spaces have the character code CHAR(32). The TRIM function is designed to detect and remove only these standard spaces. However, data copied from websites or emails often contains non-breaking spaces, represented by CHAR(160) (often used in HTML as  ). Because these are technically different characters, TRIM ignores them completely.

The Fix: Combining TRIM and SUBSTITUTE

To eliminate these stubborn non-breaking spaces, you must first convert them into standard spaces using the SUBSTITUTE function, and then wrap the entire expression in TRIM. Here is the formula:

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

How this works:

  1. SUBSTITUTE(A2, CHAR(160), " ") searches cell A2 for any non-breaking spaces (code 160) and replaces them with standard spaces (code 32).
  2. The outer TRIM function then sweeps through the text, removing any leading or trailing spaces and collapsing multiple consecutive standard spaces into single spaces.

The Ultimate Data Cleaning Formula

If you regularly import highly volatile data, you might also encounter non-printable characters, such as line breaks, carriage returns, or tab characters. To build a robust, "bulletproof" formula that handles almost any spacing and formatting mess, you can combine TRIM, CLEAN, and SUBSTITUTE:

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

Breaking Down the Ultimate Formula

  • SUBSTITUTE(A2, CHAR(160), " "): Converts web-based non-breaking spaces into regular spaces.
  • CLEAN(...): Removes the first 32 non-printable characters in the 7-bit ASCII code system (values 0 through 31), which include system-level line breaks and carriage returns.
  • TRIM(...): Performs the final pass to strip out extra leading, trailing, and internal spaces.

How to Remove ALL Spaces from a Cell

There are scenarios where you do not want to keep even a single space. For example, when cleaning product codes, credit card numbers, phone numbers, or zip codes, you might want a continuous string of characters.

To strip every single space from a text string, use the SUBSTITUTE function to replace spaces with an empty string (""):

=SUBSTITUTE(A2, " ", "")

If your dataset contains non-breaking spaces as well, nest the substitute functions together:

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

This nested formula will systematically strip out both standard and web-based spaces, leaving you with an unbroken string of characters.

Step-by-Step Workflow to Clean Your Data

Applying these formulas to large datasets requires a safe, structured workflow to ensure you do not overwrite critical source data. Follow these steps:

Step 1: Insert a Helper Column

Never write your formula directly over your raw data. Insert a new, blank column next to the messy column. Label it "Cleaned [Column Name]".

Step 2: Enter and Flash-Fill the Formula

Assuming your messy data starts in cell A2, enter your chosen formula (e.g., the ultimate cleaning formula) in B2:

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

Press Enter, then double-click the fill handle (the small square in the bottom-right corner of cell B2) to instantly apply the formula down the entire column.

Step 3: Convert Formulas to Hard Values

Because these are dynamic formulas, they still rely on the original messy column. To finalize your data cleaning:

  1. Select the entire cleaned column (Column B).
  2. Press Ctrl + C to copy the data.
  3. Right-click on the selection and choose Paste as Values (the icon with "123").

Now, the formulas are gone, and you are left with permanent, clean text strings. You can safely delete the original messy Column A.

Non-Formula Alternatives for Removing Spaces

If you prefer not to write formulas, Excel offers built-in utility tools that can achieve similar results quickly.

Method 1: Find and Replace (The Quick Fix)

If you only have standard multiple spaces (no non-breaking spaces), you can use Excel's Find and Replace dialog:

  1. Highlight the target column.
  2. Press Ctrl + H to open the Find and Replace dialog box.
  3. In the Find what box, type two spaces.
  4. In the Replace with box, type one space.
  5. Click Replace All.
  6. Repeat this process until Excel displays a message saying it made 0 replacements. This ensures that triple or quadruple spaces are fully reduced to single spaces.

Method 2: Power Query (The Automation Route)

For repeating reports where you import the same messy data files every week, Power Query is the superior option:

  1. Select your data table and go to the Data tab, then click From Table/Range.
  2. Once the Power Query Editor opens, right-click the header of the messy column.
  3. Go to Transform > Trim. Power Query will instantly strip out leading and trailing spaces.
  4. To clean internal spaces, you can use the Replace Values option to convert custom space characters.
  5. Click Close & Load to send the perfectly clean data back to your Excel sheet.

Summary Comparison of Methods

Method / Formula Removes Leading/Trailing Collapses Multiple Spaces Removes Non-Breaking Spaces (CHAR 160) Best For...
=TRIM() Yes Yes No Standard, everyday Excel cleaning.
=TRIM(SUBSTITUTE()) Yes Yes Yes Data copied from websites, emails, and web applications.
=TRIM(CLEAN(SUBSTITUTE())) Yes Yes Yes Raw data exports containing system line breaks and tabs.
=SUBSTITUTE() N/A (Removes All) N/A (Removes All) No (unless nested) Formatting IDs, serial keys, and phone numbers.
Power Query Trim Yes No (requires replacement steps) Yes Automating repeating reports and importing huge datasets.

Conclusion

Irregular spacing is one of the most common data entry discrepancies, but it is also one of the easiest to fix once you know the mechanics behind Excel's spatial formulas. By utilizing the TRIM function alongside SUBSTITUTE and CLEAN, you can eradicate stubborn non-breaking spaces and hidden characters, ensuring your lookups run flawlessly and your reports remain accurate.

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.