Trimming Off All Spaces

by Allen Wyatt
(last updated December 16, 2017)


Sudhakar has some data in column A that has trailing spaces. If he uses the TRIM function on the data, it removes some trailing spaces, but not all of them. He wonders why some spaces would be skipped and how he can remove them all.

There are actually a few different ways you can approach this problem. The right method for your purposes will depend, invariably, on the nature of the data you are using.

First of all, if you use TRIM on a cell, remember that it only removes spaces. This, by definition, means it only removes characters that have the ASCII code of 32. Unfortunately, there are other ASCII characters that show up as "spaces," but aren't really spaces. These won't be removed by TRIM.

It would help if you could figure out what those pesky characters are, right? Well, if the text in the cell isn't that long, here's a handy little macro that will look at the cell contents and display each character, in turn, along with its ASCII value:

Sub StringContents()
    Dim sTemp As String
    Dim sMsg As String
    Dim J As Integer
    If Selection.Cells.Count > 1 Then
        sMsg = "Please select only one cell"
        sMsg = "Full string: >" & ActiveCell.Value & "<" & vbCrLf
        For J = 1 To Len(ActiveCell.Value)
            sTemp = Mid(ActiveCell.Value, J, 1)
            sMsg = sMsg & ">" & sTemp & "<     " & Asc(sTemp) & vbCrLf
        Next J
    End If
    MsgBox sMsg
End Sub

To use the macro, just select the single cell you want to test and then run it. You end up with a message box that shows the full string along with each individual character within the string.

Once you know the ASCII value of the offending character that TRIM won't remove, you can do a substitution for that character. For instance, if the non-space space shows an ASCII value of 160, you could do the substitution in this manner:


This substitutes the actual space character for any ASCII 160 characters in the string. You can similarly remove other similar bogus spaces.

Of course, you can try to use a different Excel function to get rid of some of the non-printing ASCII characters, such as this:


It won't get rid of everything, but it does a good job of cleaning up a lot of the offending characters.

You can even start combining functions in your cleanup formula, in this manner:


You could take it a step further, in this manner:


This formula gets rid of the ASCII 160 characters as well as things caught by CLEAN, and then does a TRIM on that result.

If you have to clean quite a few cells or if you need to clean the cells regularly, you may want to consider using a macro to do the heavy lifting. The following macro will get rid of lots of non-printing characters, leaving just the visible characters and punctuation.

Sub CleanCells()
    Dim rTarget As Range
    Dim c As Range
    Dim sTemp As String
    Dim J As Integer
    Set rTarget = Selection.SpecialCells(xlCellTypeConstants, 2)
    For Each c In rTarget
        sTemp = c.Value
        For J = 1 To 31
            sTemp = Replace(sTemp, Chr(J), " ")
        Next J
        For J = 127 To 255
            sTemp = Replace(sTemp, Chr(J), " ")
        Next J
        c.Value = sTemp
    Next c
End Sub

The macro works only on those cells within the current selection that contain constant values. In other words, those cells which don't contain formulas.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5132) applies to Microsoft Excel 2007, 2010, 2013, and 2016.

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


Removing All Bookmarks

Need to get rid of a lot of bookmarks all at once? Word doesn't provide a way to do it, but you can use the short macro ...

Discover More

Conditional Calculations in Word

Word allows you to insert simple formulas, using fields, in table cells. You can also create simple conditional ...

Discover More

Self-Deleting Macros

Macros are very powerful, but you may not want them to always be available to a user. Here are some ways you can limit ...

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)

Using AutoComplete with Disjointed Lists

AutoComplete can help you to more quickly enter information in a worksheet. How it works, behind the scenes, can affect ...

Discover More

Enhanced Filling

Using the AutoFill feature of Excel is very handy. If you want to expand the utility offered by the feature, all you need ...

Discover More

Clearing Everything Except Formulas

Need to get rid of everything in a worksheet except for your formulas? You can do it rather easily by applying the ideas ...

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


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 six more than 8?

2017-12-17 11:24:28

Willy Vanhaelen

Better name the macro CleanCells instead of StringContents.

2017-12-17 11:19:48

Willy Vanhaelen

The macro CleanCells in this tip does a bad job. It replaces all characters above 126 with a space. So all accented characters in many languages will be replaced with a space resulting in crippled text. Here is a one line macro which does a better job quite efficiently:

Sub StringContents()
Selection = Evaluate("IF({1},TRIM(SUBSTITUTE(CLEAN(" & Selection.Address & "),CHAR(160),"" "")))")
End Sub

It is based on the last formula of this tip =TRIM(SUBSTITUTE(CLEAN(A2),CHAR(160)," ")) which does a good job but has the disavantage of needing a helper column.

2017-12-16 08:42:37

Michael (Micky) Avidan

...and so - the title of the above tip should read: "TRIMMING OFF ALLl SPACES AND/OR THAT LOOK AND BEHAVE LIKE SPACES".
"Space" (in Excel) is known as CHAR(32) and nothing else.
Therefor, the TRIM Function is capable to delete all those CHAR(32) spaces.
Non Char(32) spaces (such as Char(160) etc...) are a completely different episode.
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” Excel MVP – Excel (2009-2018)

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

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.