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

Removing Spaces

by Allen Wyatt
(last updated July 21, 2015)

3

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 (3037) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Accessing Paragraphs in a Macro

Need to process a document, paragraph by paragraph, in a macro? It's easy to do once you understand that Word's object model ...

Discover More

Avoiding a Section Break Booby Trap

Section breaks got your document formatting all messed up? It could be because of the way you added the section breaks in the ...

Discover More

Opening Sites in a Browser

You can store all sorts of information in a worksheet, including Web addresses. If you want to open those addresses in a ...

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 (MENU)

Combining Columns

Need to concatenate the contents in a number of columns so that it appears in a single column? Excel has no intrinsic way to ...

Discover More

Creating New Windows

If you need to look at different parts of the same worksheet at the same time, the answer is to create windows for your data. ...

Discover More

Moving and Copying Cells

At the very heart of editing is the ability to move and copy cells in a worksheet. Understanding the differences between ...

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 for this tip:

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

2016-06-18 05:43:51

Andres

hello Fergie, I think the following macro will help you to get what you want.

Sub CleanSpaces()
Dim Place, WorkRange
Place = Selection.Address
Dim a As Range
Set WorkRange = Selection
For Each a In WorkRange.Areas
a.Formula = Application.Trim(a.Formula)
Next a
Range(Place).Select
End Sub


2016-06-13 18:22:15

Fergie

This seems to have limited use. If I understand what I am reading, the macro would render this text as:
"Thisseemstohavelimiteduse.IfIunderstandwhatIamreading,themacrowouldrenderthistextas:"

Is there a way to delete only the spaces between paragraphs in a cell?

For example, when you pull a csv into Excel that contains scan results from a tool, say AppDetective. Some cells in that resulting spread sheet will have several paragraphs, and several empty "lines" between each paragraph.

How to we get rid of those spaces but leave the text readable?


2014-11-16 21:45:13

Mansoor Pirwa

I used find and replace command on selected cell. In find just one space and replace left blank


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.

Links and Sharing
Share