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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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

Copying Headers and Footers

Need to get headers and footers from one document to another? You can use the steps in this tip to help make quick work ...

Discover More

Printing a Worksheet List

Want a list of all the worksheets in your workbook? Here's a short, handy macro that will place all the worksheet names ...

Discover More

Specifying Your Target Monitor

When using Word to create content that will end up on the Web, it is helpful to know the probable screen resolution of ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Automatically Adding 20% to an Entry

When you are developing a worksheet for others to use, you may want to have entries in a particular cell (or cells) be ...

Discover More

Switching Editing Location

Excel allows you to edit the contents of a cell in two places—the cell itself or in the Formula bar. If you want to ...

Discover More

Setting a Default for Shifting when Inserting

When you insert cells into a worksheet, Excel needs to know which direction it should shift the displaced cells. If you ...

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 one less than 6?

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.