How to Remove Leading and Trailing Spaces in Excel
To remove leading and trailing spaces in Excel, use the TRIM function. If your text is in cell A2, type =TRIM(A2) in an empty cell and press Enter. TRIM removes all spaces before and after the text and leaves just one space between words.
Here’s the quick version:
- In an empty column, type =TRIM(A2) (use the cell with the messy text).
- Press Enter and copy the formula down.
- Copy the results and paste them back as values if you want to replace the original text.
TRIM didn’t remove the spaces? Then they’re probably non-breaking spaces (common in data copied from web pages). Use =TRIM(SUBSTITUTE(A2,CHAR(160)," ")) instead. I’ll explain it below 👇
I’ll also show you how to remove only the leading or only the trailing spaces, how to remove ALL spaces, and how to get rid of line breaks and other non-printable characters.
If you want to tag along, download the practice workbook here.
Table of Contents
Remove extra spaces before, after, and between text and numbers
Leading and trailing spaces are easy to miss, but they break lookups, sorting and filtering, because “Abigail Howard ” isn’t the same as “Abigail Howard” to Excel 😢
That’s what the TRIM function is for.
The TRIM function removes all spaces from text except for single spaces between words.
TRIM has only one argument, which is the TEXT.
It is the text from which you want spaces removed.
It doesn’t completely remove the spaces between words and numbers. It keeps one 😉
And in most cases, that’s exactly what we want.
Now, let’s use the Trim function so that I can show you what I mean 😉
- Using the sample workbook, select cell E2.
This is where we are going to type in the TRIM formula

- In Cell E2, type in =TRIM(

- Add cell C2 and a close parenthesis
)to finish the TRIM formula.
Cell C2 contains the full name information. And it will be our data for the TEXT argument. And as you can see, cell C2 has multiple spaces in it.

4. Press Enter.

As shown in the above formula, the leading spaces have been removed.
The in-between spaces have been reduced as well and now, only a single space remains.
But did you know that our sample, Abigail Howard, has trailing spaces?

It is not obvious but the TRIM function was able to remove that double space as well 😎
To replace the original text with the cleaned text, copy the TRIM results, then right-click the original cells and choose Paste Values. Then you can delete the helper column. Here’s more on copying values instead of formulas.
Remove only leading or only trailing spaces
TRIM always does three things at once: it removes leading spaces, removes trailing spaces, and squeezes double spaces between words into one.
Most of the time that’s great. But sometimes the spaces inside the text are on purpose (like in codes or fixed-width data), and you only want to remove the spaces at the beginning or at the end of the cell.
In Excel for Microsoft 365, the REGEXREPLACE function does that:
- Only leading spaces (spaces before the text):
=REGEXREPLACE(A2,"^ +","") - Only trailing spaces (spaces after the text):
=REGEXREPLACE(A2," +$","")
The pattern "^ +" means “one or more spaces at the start of the text”, and " +$" means “one or more spaces at the end”. Both are replaced with "" (nothing).
Don’t have REGEXREPLACE? This formula removes only the leading spaces in any version of Excel:
=MID(A2,FIND(LEFT(TRIM(A2),1),A2),LEN(A2))
TRIM finds the first real character, FIND looks up where that character sits in the original text, and MID returns everything from there.
Remove ALL spaces before, after, and between text and numbers
But what if you want to remove all the spaces completely?
Including the spaces between words along with trailing spaces and leading spaces.
Sadly, the TRIM function will not be able to help in this case but do not fret my friend!
Excel still has the SUBSTITUTE function 😲
SUBSTITUTE function replaces existing text with new text in a text string.
It has three mandatory arguments and one optional argument:
- TEXT: the reference to a cell containing text in which you want to substitute characters
- Old_text: the original data to replace
- New_text: the text data to replace Old_text with
- Instance_num: specifies which occurrence of Old_text to replace. If omitted, every instance of Old_text is replaced
Let’s use the sample workbook again to demonstrate how to use it.
Refer to cell D2

As shown above, this data has extra spaces all over it!
Trailing spaces, leading spaces, two in-between spaces. A total mess 😨
But stay with me and we can clean this data together by removing all that spaces.
Let’s start!
As mentioned, we will be using the SUBSTITUTE function.
- Go to Cell F2

- Type in =SUBSTITUTE(

- Insert Cell D2 and a comma: D2,

- Next, add
" "and a comma:" ",
Why " ", you ask?
It is simply because " " represents the space character in Excel 🤯

- Lastly, add
""and a close parenthesis:"")
I can feel you asking again, why ""?
Because "" represents a null value or nothing in Excel 😉

- Press Enter

As you can see, all of the spaces have been removed!
But how does that actually work?
Let me explain it to you 😁
We used all three mandatory arguments, right?
- TEXT: Cell D2
- Old_text:
" "(the space character) - New_text:
""(the null value)
What Excel did is it looked for all the space characters on the data (as set on the Old_text argument).
Then it replaced those with the null value (as set on the New_text argument).
Pretty neat, isn’t it?
No formula needed: you can also remove all spaces with Find and Replace. Select the cells, press Ctrl + H, type a single space in Find what, leave Replace with empty and click Replace All. This changes the cells directly, so it’s a good idea to keep a copy of the original data.
Removing spaces is now a piece of cake for you 🍰
Remove non-printable characters and other codes
TRIM function and SUBSTITUTE function are very useful in removing spaces.
But what if we want to remove other stuff aside from spaces in Excel?
Did you know that we can also get non-printable characters when pasting data from an external source?
This usually happens if we copy data from web pages.
Sometimes, there are even line breaks that we will not notice until we select the cell!
And not noticing them early will surely affect your outputs 😭
But how do we deal with those?
TRIM can’t help with that, because it only removes normal spaces.
Those are special character codes, and TRIM wasn’t designed for them.
But don’t you worry my friend as Excel still has your back 😎
This time, we will be using the CLEAN function 🧹
The CLEAN function removes non-printable characters from text: the first 32 characters in the ASCII code (codes 0 to 31), which include line breaks (CHAR(10)) and tabs (CHAR(9)).
It has only one argument which is the TEXT.
TEXT is any data from which you want to remove non-printable characters.
Before we start, let us revisit the sample workbook.
I will tell you something that you might not have noticed beforehand 🙀
If you select Cell C2, you will immediately see that there is a line break in it!


Sadly, our TRIM function was not able to remove this line break.
You can verify this by copying cell E2.
This is the cell we used for the TRIM formula.
Then use paste special, paste as values on the same cell.

Now, let’s get down to business!
We are going to clean this data using the CLEAN function 😏
- Using the sample workbook provided, select Cell G2

- Type in =CLEAN(

- Add cell E2 next and complete the formula with a close parenthesis:
E2)

We used the trimmed values from Cell E2 as our reference.
This is to make sure that all extra spaces have been removed as well.
- Press Enter

They are completely identical at first glance right?
But don’t let your eyes fool you!
Try to copy and paste special cell G2.
You will then see that it no longer has the line break 😳
Cell E2:

Cell G2:

To clean up spaces and line breaks in one go, combine the two functions: =TRIM(CLEAN(A2)).
TRIM not removing spaces? Fix non-breaking spaces
If TRIM leaves some leading or trailing spaces behind, they are most likely non-breaking spaces.
TRIM was designed to remove only the normal space character (character code 32). Text copied from web pages often contains the non-breaking space instead (character code 160). It looks exactly the same, but TRIM ignores it.
The fix is to turn the non-breaking spaces into normal spaces with SUBSTITUTE first, and then TRIM:
=TRIM(SUBSTITUTE(A2,CHAR(160)," "))
CHAR(160) returns the non-breaking space. SUBSTITUTE replaces every one of them with a normal space (" "), and TRIM removes the spaces at the start and end.
For really messy data (spaces, non-breaking spaces and line breaks), use all three:
=TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," ")))
Not sure what’s hiding in a cell? =LEN(A2) counts every character, spaces included. If LEN(A2) is bigger than LEN(TRIM(A2)), TRIM still has spaces to remove. If they’re the same but the text still looks padded, look for CHAR(160).
Cleaned numbers still won’t add up? TRIM and SUBSTITUTE always return text, even when the result looks like a number. Wrap the formula in VALUE to turn it into a real number, like =VALUE(TRIM(A2)). More in my guide to the VALUE function.
That’s all! Now what?
I commend you for making it this far! It was fun, right?
Now you can remove leading and trailing spaces with TRIM, remove only the leading or trailing ones, remove all spaces with SUBSTITUTE or Find and Replace, and clean out line breaks and non-breaking spaces 🧹
That’s a great skill when you deal with big data sets, or data copied from web pages and other systems.
Want to learn more of Excel’s most useful functions, like VLOOKUP, IF and SUMIF? They’re all in my free 30-minute email course 📧
Other resources
Since we are on the matter of cleaning and manipulating data, how about I offer you another article?
The CONCATENATE function for example 😎
Once you have cleaned your data, you can combine them using CONCATENATE!
Or perhaps, after cleaning the data you need to create a dropdown out of them? Then this Data Validation article is for you 😊
Want to replace more than spaces? Check out Find and Replace in Excel and the REPLACE and SUBSTITUTE functions.
Frequently asked questions
How do I remove leading spaces in Excel?
Use =TRIM(A2). It removes the spaces before the text (and after it, and extra spaces between words). To remove only the leading spaces and leave everything else alone, use =REGEXREPLACE(A2,"^ +","") in Excel for Microsoft 365.
How do I remove trailing spaces in Excel?
=TRIM(A2) removes them too. To remove only the spaces at the end of the text, use =REGEXREPLACE(A2," +$","").
Why is TRIM not removing the spaces?
They’re probably non-breaking spaces (character 160), which TRIM ignores. Use =TRIM(SUBSTITUTE(A2,CHAR(160)," ")). See TRIM not removing spaces.
How do I remove all spaces from a cell?
Use =SUBSTITUTE(A2," ",""), or select the cells, press Ctrl + H, type a space in Find what, leave Replace with empty and click Replace All.
How do I remove spaces in the whole column without a helper column?
Use Find and Replace (Ctrl + H) if you want to remove every space. If you need TRIM, put the formula in a helper column, then copy it and paste it over the original column as values.
How do I remove a line break inside a cell?
Use =CLEAN(A2), or =TRIM(CLEAN(A2)) to remove extra spaces at the same time.
