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

Copying Form Field Contents

Are you developing a form with Word? In some instances it is advantageous to copy whatever is entered in a form field to ...

Discover More

Word 2010 Mail Merge Magic (Table of Contents)

The mail merge tool available in Word is powerful, allowing you to use data from a variety of sources to create hundreds or ...

Discover More

ExcelTips: The Macros

Macros provide a way for you to extend the capabilities of Excel. The key to macros is understanding how VBA works. ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Jumping to a Range

Need a quick way to jump to a particular part of your worksheet? You can do it by using the Go To dialog box.

Discover More

Getting Rid of Spaces in Cells

Importing data into Excel that was generated in other programs can have some interesting side effects. For instance, you may ...

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
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 four less than 9?

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.