IRONSOFTWAREHOME
EXCEL 工具

How to Remove Spaces in Excel: .NET10 Guide (Methods & Formulas)

Curtis Chau
Curtis Chau
Updated: 2026年8月16日

Unwanted spaces in Excel can cause formula errors, break VLOOKUP references, and mess up data sorting. Whether you import data from web pages, copy data from external sources, or handle human data entry, extra spaces are a common nuisance.

In this tutorial, you will learn every effective method to edit your Excel files and remove spaces, from simple built-in tools to advanced formulas, Power Query, and programmatic solutions.

Why Unwanted Spaces Cause Issues in Excel

Spaces in Excel aren't always visible, but Excel treats a space as an actual character. Even a single trailing space can turn a matching formula into a frustrating error.

Common Problems Caused by Extra Spaces

  • Formula Errors: VLOOKUP, XLOOKUP, or MATCH formulas fail because "John Doe " does not equal "John Doe".
  • Incorrect Calculations: Functions like COUNTIF or SUMIF miscalculate because extra blanks create separate unique categories.
  • Sorting & Filtering Glitches: Sorting alphabetically places entries with leading spaces at the very top, breaking your expected layout.

Method 1: The Quickest Way (Find & Replace Feature)

If you need to quickly remove all spaces in selected cells, Excel's Find & Replace tool is the fastest route.

How to Mass Delete Blank Spaces

  1. Highlight the selected cells or the whole column containing extra spaces.

  2. Press Ctrl + H to open the Find & Replace dialog box.

    Open the Find & Replace dialog box

  3. In the Find what field, press the Spacebar once to enter one space.

    Finding cells with extra space

  4. Leave the Replace with field completely empty.

  5. Click Replace All.

    Empty spaces successfully removed

提示: If you only want to remove spaces between words while retaining single spaces, do not use Find & Replace, as it deletes every single space in the range.

Method 2: Using the TRIM Function (Standard Cleaning)

The TRIM function is designed specifically to remove leading and trailing spaces from a text string while leaving single spaces between words intact.

How the TRIM Function Works

  • Removes leading spaces: Deletes all spaces before the first character.
  • Removes trailing spaces: Deletes every space after the last character.
  • Fixes spaces between words: Reduces multiple spaces down to single spaces.

Step-by-Step Formula Implementation

Initial Cell with Empty Space

Cell with empty cell

  1. Insert a helper column next to your original column.

  2. In the first cell of the helper column (e.g., B2), enter the formula:

    =TRIM(A2)
    Text
  3. Press Enter to execute the formula.

    Executing the formula

  4. Drag the fill handle down to apply the formula across the entire column.

  5. Copy the newly cleaned range, right-click the original column, and choose Paste as Values to save the clean text string over the raw data.

    New value without the spac

Method 3: Removing Non-Breaking Spaces (TRIM + SUBSTITUTE)

Does TRIM() remove all spaces? No. A common issue when you import data or copy data from web pages is the presence of non-breaking spaces (ASCII code 160). The standard TRIM function only recognizes standard spaces (ASCII code 32) and will completely ignore non-breaking spaces.

Formula to Remove Non-Breaking Spaces and Extra Spaces

To clean non-breaking spaces, combine the SUBSTITUTE function, CHAR function, and TRIM function:

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

Adding the formula

How This Formula Works

Output

  • SUBSTITUTE(A2, CHAR(160), " "): Finds non-breaking spaces using their code number (160) and replaces them with standard single spaces.
  • CLEAN(...): Uses the CLEAN function to strip out non-printable characters and line breaks (ASCII 0–31).
  • TRIM(...): Strips out all remaining leading and trailing spaces and fixes spaces between words.

Method 4: Removing Spaces via Power Query

For recurring data imports or large datasets, Power Query automates the data cleaning process without relying on complex cell formulas.

  1. Select your data range or table.
  2. Go to the Data tab and click From Table/Range.
  3. Right-click the column header you wish to clean.
  4. Select Transform > Trim to remove leading and trailing spaces.
  5. Select Transform > Clean to strip out invisible characters and line breaks.
  6. Click Close & Load to return the cleaned dataset into a new worksheet.

Method Comparison Table

MethodLeading & Trailing SpacesMultiple Spaces Between WordsNon-Breaking Spaces (ASCII 160)All Spaces
Find & ReplaceDeletesDeletesNoDeletes
TRIM FunctionRemovesReduces to 1NoNo
TRIM + SUBSTITUTERemovesReduces to 1RemovesNo
SUBSTITUTE FunctionDeletesDeletesManualDeletes
Power QueryRemovesRetainsHandles via scriptOptional

For Developers: Automating Excel Space Removal with IronXL

While manual ribbon commands and cell formulas work well for desktop spreadsheets, automating batch updates across thousands of workbooks requires a developer-focused tool. IronXL is a robust .NET spreadsheet processing library that lets C# and VB.NET developers clean Excel files programmatically without requiring Microsoft Excel to be installed.

Cleaning Cell Strings Programmatically

Using IronXL in C#, you can iterate through cell ranges to strip out unwanted spaces, non-printable characters, and formatting errors on server environments:

using IronXL;

// Load an existing workbook containing raw imported data
WorkBook workbook = WorkBook.Load("ImportedData.xlsx");
WorkSheet worksheet = workbook.DefaultWorkSheet;

// Loop through rows in a target column to clean up text strings
foreach (var cell in worksheet["B2:B100"])
{
    if (cell.Value != null)
    {
        string rawText = cell.Value.ToString();

        // Remove non-breaking spaces (ASCII 160) and standard extra spaces
        string cleanedText = rawText.Replace((char)160, ' ').Trim();

        // Update the cell value
        cell.Value = cleanedText;
    }
}

// Save the updated, clean workbook
workbook.SaveAs("CleanedData.xlsx");
Text

IronXL Output

IronXL Output

Summary

Removing unwanted spaces in Excel is essential for accurate calculations, error-free formulas, and clean data analysis. You can quickly delete all spaces using Find & Replace, eliminate excess spaces while preserving word separation using the TRIM function, or handle complex web data imports by combining TRIM, CLEAN, and SUBSTITUTE to clear non-breaking spaces (ASCII 160).

Streamline Your Excel Workflows with IronXL

Need to automate spreadsheet operations, clean high-volume data, or manage Excel reports directly within your .NET applications? IronXL simplifies workbook manipulation with clean, intuitive C# code.

Start Your IronXL Free Trial Today

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

相關文章

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預約您的免費即時演示
Booking Badge

全球數百萬工程師的信賴

Iron Software的客戶標誌
獲得無義務諮詢
填寫以下表格或電郵sales@ironsoftware.com
您的資料將始終保密。
全球數百萬工程師的信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立