Skip to main content

How to Remove Spaces in Excel: TRIM(), SUBSTITUTE() and More

Remove unnecessary spaces, including leading, trailing, and extra spaces, to keep your data clear. This guide covers TRIM(), SUBSTITUTE(), and CLEAN() functions, plus practical tips to ensure spreadsheets remain accurate and easy to work with.
· 11 min read

Explore with AI

Add DataCamp as preferred source on Google

I once got a spreadsheet with 500+ entries that looked fine initially but wouldn’t calculate totals correctly. I didn’t know why, but later, it turned out that hidden spaces were causing all sorts of trouble. If you handle data in Excel, you may have encountered similar situations. These spaces often come when you import data from other sources.  

The short answer: =TRIM(A2) removes leading, trailing, and repeated spaces and keeps one space between words. =SUBSTITUTE(A2," ","") removes every space, and Find & Replace (Ctrl+H) cleans cells in place without a formula. Below, I walk through each method with examples, plus the fixes for the characters TRIM() can’t see. However, if you’re just starting, check out our Data Analysis in Excel course or the Excel Fundamentals skill track. 

TL;DR

  • Hidden spaces break formulas — leading, trailing, or extra spaces cause VLOOKUP() mismatches, SUM() returning zero, and failed comparisons.
  • TRIM() — use for most cases; removes leading/trailing/extra spaces, keeps one space between words.
  • SUBSTITUTE() — use to remove ALL spaces including between words, or to target specific characters.
  • CLEAN() — combine with TRIM() to strip non-printable characters from imported data.
  • Non-breaking spaces? Use =TRIM(CLEAN(SUBSTITUTE(A1,CHAR(160)," "))) — the all-in-one fix.
  • Find & Replace (Ctrl+H) — fastest option for removing spaces directly in-place, no helper column needed.

Understanding Spaces in Excel

Before we remove spaces, let’s understand the types of spaces that may be lurking in your data:

  • Leading spaces: Spaces at the start of a cell.
  • Trailing spaces: Spaces at the end of a cell.
  • Extra spaces between words: More than one space between words.
  • Non-breaking spaces: Spaces with character code 160 (CHAR(160)) that look normal, but TRIM() ignores them. They usually arrive with data copied from web pages.

These spaces can slip into your data from importing files, manual entry mistakes, inconsistent formatting, or even copying and pasting from other sources.

different types of spaces in Excel

Types of spaces. Image by Author. 

Different Ways to Remove Spaces in Excel

Stray spaces break lookups like VLOOKUP(), make identical-looking values fail comparisons, and turn numbers into text that SUM() ignores. Before you clean anything, I recommend two checks:

  • Back up your data: Always make a copy of your data before making changes. This ensures you can recover the original data if something goes wrong.
  • Check formatting: Ensure consistent formatting in your cells, such as text case and number formats, for cleaner and more reliable results.

Now, let’s explore the best methods to remove spaces in Excel.

Method 1: Use the TRIM() function to remove extra spaces

The TRIM() function removes unnecessary spaces, such as leading, trailing, and multiple spaces between words, except for the single-space character. For a complete reference, see our guide to the TRIM() function in Excel. Here's how to use it:

  1. Create a helper column next to your data.

  2. In the first cell of the helper column, enter the formula: =TRIM(A2).

  3. Press Enter and drag the formula down to apply it to all rows.

Using TRIM function to remove all the spaces in Excel.

Use TRIM() to remove the spaces. Image by Author.

Tip: replace the original data. After applying TRIM() in the helper column, copy the trimmed cells, then right-click the original column and select Paste Special > Values (shortcut: Ctrl+Alt+V, then V and Enter on Windows, or Ctrl+Cmd+V on Mac). This pastes just the cleaned values, letting you delete the helper column afterward.

Method 2: Use the SUBSTITUTE() function

The SUBSTITUTE() function replaces one piece of text with another. Replace a space with nothing ("") and it removes every space in the cell, including the ones between words. That makes it the right tool for phone numbers, IDs, and numbers with spaces between digits. 

Its basic syntax is:

=SUBSTITUTE(text, old_text, new_text, [instance_num])

Here’s how to apply it and remove all the spaces: 

  1. Create a helper column.

  2. Enter the formula: =SUBSTITUTE(A2," ","").

  3. Press Enter and drag it down to apply it to all rows.

all the spaces are removed using substitute function in excel.

Using the SUBSTITUTE() function. Image by Author.

Method 3: Data cleanup with Find & Replace

If you want a quick and easy fix, I’d recommend Excel’s Find & Replace tool — it can handle extra spaces between words. Here’s how:

  1. Select your data range.

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

  3. In the Find what field, enter two spaces.

  4. In the Replace with field, enter a single space.

  5. Click Replace All.

using Find & replace feature in Excel

Cleaning spaces using the Find & Replace feature. Image by Author.

Repeat the steps until Excel shows the message, We couldn't find anything to replace. This method shrinks every run of spaces, including leading and trailing ones, to a single space, but it never removes that last space. To delete every space, search for one space and leave Replace with empty; that also joins words together, so use it only on codes, IDs, or numbers. 

Method 4: Use the CLEAN() function to remove non-printing characters

When we import or copy data from external sources, we often get non-printable characters that can disrupt data operations. You can use the CLEAN() function to remove them or, even better — combine it with TRIM() to clean up extra spaces.

Here’s how you can do it: 

  1. Enter the formula: =TRIM(CLEAN(A2)) in a helper column.

  2. Press Enter and drag the formula down.

use CLEAN and TRIM functions together to remove spaces in Excel.

Combine TRIM() and CLEAN() to remove spaces. Image by Author.

Method 5: Advanced method – use Excel Add-Ins for faster results

You can use the third-party add-in Ablebits Ultimate Suite for Excel. Its Trim Spaces tool cleans up data quickly. With one click, it takes care of everything. This includes leading and trailing spaces, line breaks, extra blanks between words, and non-breaking spaces. So, if you’re like me who works with large datasets regularly, it can save you so much time. 

Here is how to use it:

  1. Select the range of cells to clean.

  2. Open the Ablebits Data tab and click Trim Spaces.

  3. From the options pane, select what you want to remove and click Trim.

TRIM spaces in Excel

Trim spaces with Excel’s add-in. Image by Author.

Method 6: Use Power Query for data you clean every time it arrives

Power Query records your cleaning steps, so the same cleanup runs again each time you refresh the data. Use it when you import the same report every week. Here’s how:

  1. Select your data and choose Data > From Table/Range to open the Power Query Editor.

  2. Select the column to clean, then choose Transform > Format > Trim to remove leading and trailing spaces. Format > Clean removes non-printing characters.

  3. Power Query’s Trim doesn’t collapse spaces between words, so use Transform > Replace Values to replace two spaces with one.

  4. Click Home > Close & Load to send the cleaned table back to Excel.

Next time, click Data > Refresh All and the steps run again on the new data. Our Introduction to Power Query in Excel course covers these transformations in more depth.

Method 7: Use REGEXREPLACE() in Excel for Microsoft 365

REGEXREPLACE() replaces text that matches a pattern, which lets you target exactly the spaces you want. It works in Excel for Microsoft 365 on Windows and Mac and in Excel for the web; older versions return #NAME?.

GoalFormula
Remove only leading spaces=REGEXREPLACE(A2,"^ +","")
Remove only trailing spaces=REGEXREPLACE(A2," +$","")
Turn regular and non-breaking spaces into single spaces=TRIM(REGEXREPLACE(A2,"[ "&CHAR(160)&"]+"," "))

In a pattern, ^ means the start of the cell, $ the end, and + one or more spaces. Our Excel regex tutorial explains the syntax.

Specific Solutions for Common Space Removal Scenarios in Excel

Now, let’s look at a few common scenarios you may run into and how you can quickly fix them.

Remove extra spaces

Extra spaces can make your data look messy and hard to work with. You can use the TRIM() function to clean them up while keeping one space between words. For example, I have a couple of data entries with inconsistent spacing. To sort this, I entered the following formula in cell B2 and applied it to all rows:

=TRIM(A2)

Use TRIM() to remove extra spaces in Excel.

Use TRIM() to remove extra spaces. Image by Author.

Remove leading spaces 

To remove only leading spaces and keep any double spaces between words, you can’t use TRIM(), because it also collapses repeated spaces inside the text. Use this formula instead:

=MID(A2,FIND(MID(TRIM(A2),1,1),A2),LEN(A2))

Here’s how the formula works: It locates the first non-space character and keeps everything from that point forward. This way, it keeps spaces between words and trims only at the start of the cell. In Excel for Microsoft 365, =REGEXREPLACE(A2,"^ +","") does the same job in a shorter formula.

removing only leading spaces in Excel.

Remove only leading spaces. Image by Author.

Remove trailing spaces

Trailing spaces are extra spaces that appear after text or numbers in a cell. These invisible characters can cause problems like formula errors, where cells that appear identical fail to match in functions like VLOOKUP() or MATCH(). To avoid this, use the TRIM() function. For example, I put this formula in cell B2 and then applied it to all rows to remove extra spaces from my data.

=TRIM(A2)

Remove trailing spaces using TRIM function in Excel.

Removing trailing spaces. Image by Author.

Remove spaces before numbers

While TRIM() works great for cleaning up spaces in text, you may face issues using it with numeric data. Let me show you what happens:

=TRIM(A2)

TRIM() function fails to remove spaces correctly in Excel.

The TRIM() function doesn’t remove the spaces correctly. Image by Author.

You can see, the TRIM() removed spaces before a number, but Excel doesn't treat the result as a number anymore — it treats the outcome as text. Here’s how to spot this:

  • The data is left-aligned instead of right.

  • Look at your status bar - it only shows you how many cells you've selected (COUNT), not their total. 

  • If you try to add up these cells with SUM(), you'll get zero.

You need to turn the text back into a number to fix this. To do so, you can either multiply by 1 or use this formula:

=VALUE(TRIM(A2))

This removes spaces and ensures Excel treats the data as numbers.

Combine VALUE() and TRIM() to remove spaces before numbers in Excel.

Combine VALUE() and TRIM() to remove spaces before numbers. Image by Author.

Remove spaces from a string 

If you want to remove all spaces, including those between words, use the SUBSTITUTE() function:

=SUBSTITUTE(A2, " ", "")

You can see the formula cuts off every space from the string.

Remove the spaces using SUBSTITUTE function in Excel.

Remove the spaces using the SUBSTITUTE() function. Image by Author.

Remove special characters and unnecessary spaces  

SUBSTITUTE() is great when you want to remove specific characters from your text. But you can pair it with TRIM() to clean up special characters and spaces in one go. For example, if I want to remove & and clean up spaces, I use this formula:

=TRIM(SUBSTITUTE(A2, "&", ""))

You can see how TRIM() and SUBSTITUTE() work when used together versus SUBSTITUTE() alone.

Combine TRIM() and SUBSTITUTE() to clean spaces in Excel.

Combine TRIM() and SUBSTITUTE() to clean spaces. Image by Author.

Remove line breaks

Sometimes data pasted from the web or other sources contains line breaks (Alt+Enter characters, CHAR(10)) that look like extra spacing. The TRIM() function won’t catch these. To remove them:

  1. Select the cells you want to clean.

  2. Press Ctrl+H to open Find & Replace.

  3. In the Find what field, press Ctrl+J (a blinking cursor appears — that’s the line break character).

  4. Leave the Replace with field empty (or type a space to avoid merging words).

  5. Click Replace All.

Alternatively, use the formula: =TRIM(CLEAN(A1)) — the CLEAN() function strips line breaks (CHAR(10)) and other non-printable characters (ASCII 1–31) in one step.

Common Problems and Solutions

Sometimes, when I copy or import data from other sources, a lot of non-breaking spaces and strange characters appear in my data. Here’s how I handle them: 

Non-breaking spaces that TRIM() doesn’t handle

The TRIM() function removes extra spaces, but it doesn’t work for non-breaking spaces (character code 160). These often show up in data from web pages or formatted sources. To fix this, I use:

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

This formula removes non-breaking spaces entirely. If you want to replace them with regular spaces first (so TRIM() can then clean up any extras), use: =TRIM(CLEAN(SUBSTITUTE(A1,CHAR(160)," "))) — the gold-standard formula for stripping all space types at once.

Hidden characters or pasted data problems

Sometimes, other hidden characters sneak in, like tabs or line breaks. To find them, I use CODE() to check the character’s position:

  • First character: =CODE(LEFT(A1,1))

  • Last character: =CODE(RIGHT(A1,1))

  • Specific position: =CODE(MID(A1, n, 1)) (Replace n with the position number.)

This is the formula I use:

=CODE(RIGHT(C2,1))

Once I know the code, I remove it with TRIM() and SUBSTITUTE(). For example, to remove tabs (code 115), I use:

(SUBSTITUTE(C2, CHAR(115), “ “))

remove the hidden spaces using CODE, TRIM and SUBSTITUTE functions in Excel.

Use CODE(), TRIM(), and SUBSTITUTE() to remove spaces. Image by Author.

Quick Method Comparison

Not sure which method to use? This table maps each scenario to the right tool:

ScenarioBest MethodFormula / Shortcut
Remove leading, trailing & repeated spacesTRIM()=TRIM(A1)
Remove ALL spaces (including between words)SUBSTITUTE()=SUBSTITUTE(A1," ","")
Quick in-place cleanup, no formula neededFind & ReplaceCtrl+H
Data imported from web or other appsTRIM() + CLEAN()=TRIM(CLEAN(A1))
Non-breaking spaces (CHAR(160)) from web pagesTRIM + CLEAN + SUBSTITUTE=TRIM(CLEAN(SUBSTITUTE(A1,CHAR(160)," ")))
Spaces mixed with numeric dataVALUE() + TRIM()=VALUE(TRIM(A1))
Remove line breaksFind & Replace or CLEAN()Ctrl+H → Ctrl+J or =TRIM(CLEAN(A1))
Only leading or only trailing spaces (Microsoft 365)REGEXREPLACE()=REGEXREPLACE(A1,"^ +","") / =REGEXREPLACE(A1," +$","")
Same cleanup on every new importPower QueryTransform > Format > Trim
Large dataset, frequent cleaning neededExcel Add-In (Ablebits)Ablebits Data > Trim Spaces

Final Thoughts

In this guide, I showed you methods like TRIM() for simple fixes, SUBSTITUTE() for advanced problems, and add-ins in case you will be removing spaces frequently and want a different process. Each method suits different data types, so choose the one that fits your needs. For a broader look at keeping your spreadsheets clean, see our guide to data cleaning in Excel.

If you want to strengthen your skills further, check out the DataCamp Data Analysis with Excel Power Tools skill track. It offers step-by-step lessons to help you master Excel and improve your data-cleaning techniques.

Laiba Siddiqui

Author

Laiba Siddiqui

I'm a content strategist who loves simplifying complex topics. I’ve helped companies like Splunk, Hackernoon, and Tiiny Host create engaging and informative content for their audiences.

FAQs About Removing Spaces in Excel

Can I use Power Query to remove spaces in Excel?

Yes. Select your data and choose Data > From Table/Range to open the Power Query Editor. Select the column, then choose Transform > Format > Trim to remove leading and trailing spaces. Power Query’s Trim doesn’t collapse spaces between words, so use Replace Values for those, then click Close & Load.

How do I remove dashes and extra spaces from a cell in Excel?

Combine the two functions: =TRIM(SUBSTITUTE(A2,"-","")). SUBSTITUTE() deletes every dash, then TRIM() removes leading, trailing, and repeated spaces. To turn each dash into a space instead, for example in phone numbers, replace it with " " rather than an empty string.

Can I remove spaces from a text string using Flash Fill in Excel?

Yes. Type the cleaned version of the first cell next to it, for example JohnDoe in B2 when A2 holds John Doe, then press Ctrl+E. Flash Fill copies the pattern down the column. The results are fixed values, so unlike a formula they won’t update when the original data changes.

Can I remove spaces and convert text to uppercase at the same time?

Yes. Nest SUBSTITUTE() inside UPPER(): =UPPER(SUBSTITUTE(A2," ","")) removes every space and returns the text in capitals. To keep one space between words, use =UPPER(TRIM(A2)) instead. This is handy for cleaning product codes or IDs before a lookup, and works the same with LOWER().

What is the fastest way to remove spaces in Excel without a formula?

Use Find & Replace (Ctrl+H). Type one space in Find what, leave Replace with empty, and click Replace All to delete every space in the selected cells. To keep one space between words, find two spaces, replace them with one, and repeat until Excel finds nothing to replace.

Why does TRIM() not work on my data?

TRIM() only removes regular spaces (ASCII 32). If your data contains non-breaking spaces (CHAR(160), common in web-pasted text) or non-printable characters, TRIM() alone won’t work. Use the all-in-one formula: =TRIM(CLEAN(SUBSTITUTE(A1,CHAR(160)," "))). You can identify the mystery character with =CODE(LEFT(A1,1)) or =CODE(RIGHT(A1,1)).

How do I remove spaces in Excel?

Use =TRIM(A2) in a helper column to remove leading, trailing, and repeated spaces while keeping one space between words. To delete every space, use =SUBSTITUTE(A2," ",""). For data copied from the web, use =TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," "))). Then copy the results and paste them back as values.

Topics

Related

⌂ 换网址