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

Adding Text to an Envelope

Need to customize the way that Word prints envelopes? There are a couple of approaches you can use, as discussed in this tip.

Discover More

Viewing Footnotes

Adding footnotes to a document is essential for some types of writing. How you view those footnotes depends on the display ...

Discover More

Moving Cells Using the Mouse

Want to easily move information from one cell to another? A quick way to do it is to simply drag and drop using the mouse.

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Preparing Data for Import into Access

When importing Excel information into Access, you need to be concerned with the condition of the data. Here's how to make ...

Discover More

Copying Cells to Fill a Range

Excel provides two really helpful shortcuts you can use to fill a range of cells, either horizontally or vertically. These ...

Discover More

Getting Rid of Non-Printing Characters Intelligently

Is your worksheet, imported from an external source, plagued by non-printing characters that show up like small boxes ...

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 7 + 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.