Excel.Tips.Net Welcome toExcel.Tips.Net

Helpful Links

Tips.Net Home
ExcelTips Home
Ask an Excel Question
Make a Comment

Tips.Net Store

ExcelTips FAQ
ExcelTips Premium

Learn Access Now
Free Printable Forms

Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips

Advertise on the
ExcelTips Site

Newest Tips

Recording a Macro

Adding a Little Animation to Your Life

Converting a Range of URLs to Hyperlinks

Making the Formula Bar Persistent

Engineering Calculations

Digital Signatures for Macros

Fixing the Decimal Point

 

Getting Rid of Non-Printing Characters Intelligently

Summary: Is your worksheet, imported from an external source, plagued by non-printing characters that show up like small boxes on-screen? You can get rid of them using the various techniques described in this tip. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)

If you work with files that originate from a non-Excel source, you can sometimes end up with characters in your cells that Excel doesn't know how to display properly. For instance, you may have a comma-delimited text file generated by your company's accounting software, and you load the file into Excel. In some cells you may notice that there are small boxes. These represent non-printing characters. Excel displays the small boxes so that you know the character is there, even though it cannot be displayed or printed.

To get rid of these characters you an try to use the Find and Replace feature of Excel. Try these steps:

  1. Within the cell that contains one of the small boxes, highlight the box and press Ctrl+C. This copies the character to the Clipboard.
  2. If you are using Excel 97 through Excel 2003, choose Replace from the Edit menu or press Ctrl+H. If you are using Excel 2007 you can also press Ctrl+H or you can click the Home tab of the ribbon, click Find & Select in the Editing group, then choose Replace from the submenu. Excel displays the Replace tab of the Find and Replace dialog box. (Click here to see a related figure.)
  3. With the insertion point in the Find What box, press Ctrl+V. This pastes the contents of the Clipboard (the offending character) into the Find What box. The character will most likely not look like the small box you selected and copied in step 1.
  4. If nothing was pasted in step 3, then close the dialog box and try the steps again. If nothing is still pasted, then you won't be able to use Find and Replace to get rid of the non-printing characters, and you can skip the rest of these steps.
  5. If you want to just delete the characters, make sure there is nothing in the Replace With box. If you want to replace the characters with spaces, put a single space in the Replace With box.
  6. Click on Replace All.

This approach may or may not work, depending mostly on Excel and whether it let you accurately copy the offending character in step 1. If it does work, then you have learned a valuable technique for getting rid of the bad characters. If it doesn't work, then you should try a different approach.

One thing to try is to use Word in your "clean up" operations. Copy the data from Excel to a Word document (paste it as regular text), and then replace the offending characters. You can then paste the data back into Excel. Some people report that they get exactly the results they want by using this round-trip approach to working with the data.

You can, of course, use a macro to get rid of the offending characters. It isn't too difficult to create your own version of the CLEAN worksheet function that, instead of simply removing non-printing characters, replaces them with spaces. Consider the following example macro:

Function ReplaceClean(sText As String, Optional sSubText As String = " ")
    Dim J As Integer
    Dim vAddText

    vAddText = Array(Chr(129), Chr(141), Chr(143), Chr(144), Chr(157))
    For J = 1 To 31
        sText = Replace(sText, Chr(J), sSubText)
    Next
    For J = 0 To UBound(vAddText)
        sText = Replace(sText, vAddText(J), sSubText)
    Next
    ReplaceClean = sText
End Function

You use this function in the following manner within your worksheet:

=ReplaceClean(B14)

In this case, all non-printing characters in cell B14 are replaced with a space. If you want the characters replaced with something else, just provide the text to replace with. The following example replaces the non-printing characters with a dash:

=ReplaceClean(A1,"-")

The following usage simply removes the non-printing characters, the same as the CLEAN function:

=ReplaceClean(A1,"")

If you look back at the ReplaceClean macro presented earlier, you see that it uses the Replace function. This VBA function is not available in all the versions of VBA used with the different versions of Excel. If you try the macro and you get an error on one of the lines that use the Replace function, then use this version of the ReplaceClean macro instead:

Function ReplaceClean(sText As String, Optional sSubText As String = " ")
    Dim J As Integer
    Dim vAddText
    Dim aWF As WorksheetFunction
    Set aWF = Application.WorksheetFunction

    vAddText = Array(Chr(129), Chr(141), Chr(143), Chr(144), Chr(157))
    For J = 1 To 31
        sText = aWF.Substitute(sText, Chr(J), sSubText)
    Next
    For J = 0 To UBound(vAddText)
        sText = aWF.Substitute(sText, vAddText(J), sSubText)
    Next
    ReplaceClean = sText
    Set aWF = Nothing
End Function

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2947) applies to Microsoft Excel versions: 97 | 2000 | 2002 | 2003 | 2007

Tame Your Data! ExcelTips: Filters and Filtering provides all the details necessary to let you manage large sets of data with confidence and ease. Its information-packed pages demonstrate how to use the two types of filters provided by Excel: AutoFilters and advanced filters.
 
Check out ExcelTips: Filters and Filtering today!