Written by Allen Wyatt (last updated January 28, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365
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"
    Else
        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:
=SUBSTITUTE(A1,CHAR(160)," ")
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:
=CLEAN(A1)
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:
=TRIM(CLEAN(A1))
You could take it a step further, in this manner:
=TRIM(SUBSTITUTE(CLEAN(A2),CHAR(160)," "))
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. And, since it replaces everything having an ASCII value of 127 or greater, you may end up with characters replaced that you don't really want replaced (such as foreign-language characters). If that occurs, then you need to evaluate your text, as discussed earlier, and change only those characters that are unwanted.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5132) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365.
 
                        Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!
Limiting what can be entered in a cell can be an important part of developing a worksheet that other people use. Here are ...
Discover MoreEach cell in a worksheet can hold quite a bit of information. If you want to see the information in the cell without the ...
Discover MoreWhen you undo actions within Excel, those steps you undo may affect the multiple workbooks in which you've been working. ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2025-04-26 23:09:17
sandeep
Great codes, Allen.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments