Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Removing Spaces.

Removing Spaces

by Allen Wyatt
(last updated November 15, 2014)

11

Do you have a lot of data that contains spaces, and you need to remove those spaces? Perhaps you imported it from another program, or the spaces were entered by mistake. For example, you may have a large number of policy numbers in a worksheet, and there are spaces in the policy numbers. If you want to remove those spaces, there are two approaches you can use.

The first approach is to use the SUBSTITUTE function. Let's say that a policy number is in cell A5. In cell B5 you could use this formula:

=SUBSTITUTE(A5," ","")

The result is that cell B5 contains the policy number with all the spaces removed.

The second approach works well if you have a lot of cells containing spaces, and you want to remove them in one step. Create the following macro:

Sub NoSpaces()
    Dim c As Range

    For Each c In Selection.Cells
        c = Replace(c, " ", "")
    Next
End Sub

Select the cells you want to modify, and then run the macro. It examines each cell in the selected range, removing any spaces in that range. The result is then placed back in the same cell.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10741) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Removing Spaces.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Turning Off Automatic Capitalization

When you start typing, do you ever notice those times that Word starts capitalizing words at the start of what it thinks ...

Discover More

Determining the Day of the Year

Need to find out the day of the year for a particular date? It's easy to do if you are using a macro. All you need to do ...

Discover More

Creating Custom Document Properties

Word allows you to keep track of any number of custom properties about a document. Here's how to create those properties ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Showing Text when a Cell is Empty

Wouldn't it be great if you could have Excel display some text in a cell only when that cell is empty? Unfortuantely, ...

Discover More

Converting Forced Text to Numbers

If you have some numbers stored in cells that are formatted as text, you may get some surprises when you try to use those ...

Discover More

Converting Text to Values

When you import information originating in a different program, Excel may not do the best job at figuring out what ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 9 - 4?

2014-11-19 08:51:56

Glenn Case

Rudra:

TRIM only acts on leading or trailing spaces, not on those contained within other text.


2014-11-19 02:27:33

Rudra Sharma

Why not use trim function?


2014-11-18 08:48:34

Bigger Don

@ Ted
What you described matches VBA's Trim() behavior. The TRIM() Worksheet Function removes excess internal spaces.

Not doubting your word, just trying to clarify. Software does weird things.


2014-11-17 22:18:58

sambit

Thanks for useful Tips.


2014-11-17 10:50:24

Ted

The trim command only works to remove excess spaces at the beginning or the end of the word. I tried it, and it did not remove the spaces between the letters.
eg
Trim ____Ab bo tt___ = Ab bo tt
Substitute ___ Ab bo tt___ = Abbot


2014-11-17 09:12:17

Scott Selvey

If it's just unwanted spaces to remove, I usually use the =trim() function. This is useful when something is copied in, and there are multiple spaces at the end. I've also used =clean() to remove unprintable characters that may appear as spaces on the screen.


2014-11-17 06:14:34

Ron Sallee

I've also used the text to columns command to remove spaces from the end of a fixed width data field.


2014-11-16 19:39:42

Jon Lettington

As Thomas suggests, sometimes the space you see might not actually be a space!

Copy the space character that refuses to be removed and then use Find & Replace, pasting the strange space character into the Find field and leave Replace with empty as described by Thomas.

Those space like characters (which are not spaces) will disappear.


2014-11-16 16:51:55

Fred

Wouldn't the "trim" function be the easiest solution?


2014-11-15 07:32:02

Dritan

Very good suggestion, excellent job.


2014-11-15 07:17:22

Thomas Papavasiliou

Another simple way is to select the cells you wish to remove the space and use the Replace command. Then in the poping up dialog box, in the box "Find what" you type a space and leave the box "Replace with" empty, finally, click the "Replace All".

Attention to the character under Ascii 160, the non breaking space, that you may encounter in imported data, it appears as the Ascii 32, that is the space entered by the space key of the keyboard


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.