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

Changing How Footnote References Appear

Footnote references normally appear as superscripted digits, both in the main body of your document and in the footnotes ...

Discover More

Using Hidden Styles

Using styles to format templates can save a lot of time and ensure consistency when working on several documents. Some styles ...

Discover More

Making Wider Footer Margins

Want the margins used in your footers (or headers) to be wider than the margins used in the rest of your document? There are ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (menu)

Checking for an Entry in a Cell

You may be looking for a way to have a formula determine if a particular cell has anything in it. Here's how you can find the ...

Discover More

Dealing with Long Formulas

If your worksheet formulas seem to go on forever, here's a handy way to make them more understandable. (All you need to do is ...

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

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. 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 6?

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


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.