Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Getting Rid of 8-Bit ASCII Characters.

Getting Rid of 8-Bit ASCII Characters

Written by Allen Wyatt (last updated March 4, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

Bill often has to import information into a worksheet so that he can process it for his company. In Bill's situation, one of the first steps he needs to do is to remove all the 8-bit ASCII characters that may be in the imported data. These characters don't need to be replaced with anything; they just need to be deleted so that only 7-bit ASCII characters remain. Bill wonders if there is an easy way to do this, perhaps with a macro of some type.

There are a few ways that you can approach this problem, depending on the characteristics of the data that you are starting with. Assuming that you only have 8-bit characters in your worksheet, then the only character codes that could be used for characters is 0 through 255. If you want to limit your data to only 7-bit characters, then that means you only want things in the character-code range of 0 through 127. Thus, you could use a macro to easily search for any characters in the range of 128 to 255 and simply delete them. This macro takes this approach:

Sub Remove8Bit1()
    Cells.Select
    For i = 128 To 255
        X = Chr(i)
        Selection.Replace What:=X, Replacement:="", _
          LookAt:=xlPart, SearchOrder:=xlByRows, _
          MatchCase:=False, SearchFormat:=False, _
          ReplaceFormat:=False
    Next
End Sub

The approach finds only those values in your worksheet that are in the 8-bit range. It won't touch anything that is in the 8-bit range that is actually created by a formula. (In most instances that shouldn't be a problem. If it is a problem, the proper fix is to modify the formulas creating the offending results.)

If your data contains Unicode characters, then you'll want to use a different approach. Technically, Unicode characters are not 8-bit characters; they are 16-bit characters and can have character code values in the range of 0 to 65,535. Because you want to ignore anything with a value over 127, using the search-based approach discussed earlier becomes unwieldy—you would end up doing over 65,000 searches instead of only 128.

A better approach is to simply look at all the characters in all the selected cells and if they have a character code over 127, ignore them. That is the approach taken in the following macro:

Sub Remove8Bit2()
    Dim rngCell As Range
    Dim intChar As Integer
    Dim strCheckString As String
    Dim strCheckChar As String
    Dim intCheckChar As Integer
    Dim strClean As String

    For Each rngCell In Selection
        strCheckString = rngCell.Value
        strClean = ""
        For intChar = 1 To Len(strCheckString)
            strCheckChar = Mid(strCheckString, intChar, 1)
            intCheckChar = Ascw(strCheckChar)
            If intCheckChar < 128 Then
                strClean = strClean & strCheckChar
            End If
        Next intChar
        rngCell.Value = strClean
    Next rngCell
End Sub

Note that the macro uses the Ascw function instead of the traditional Asc function so that it looks at Unicode values.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10565) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Getting Rid of 8-Bit ASCII Characters.

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

Inserting Symbols

Using the Character Map to insert symbols in Excel.

Discover More

Quick Recall of Table Formats

Got a table that you use over and over again? One way you can make quick work of such repetition is to save the table in ...

Discover More

Defining Default Printers on a Document Level

If you use multiple printers, you may wonder how to set each document in Word to remember which printer to use for that ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Importing a Subset of Records

If you only want to import a portion of whatever records are in a text file, Excel provides a number of ways you can ...

Discover More

Changing Limited Relative References to Absolute

Do you need to change whether a particular reference in a formula uses a relative or absolute reference? If so, you may ...

Discover More

Setting an Upper Threshold for a Cell

Do you want to limit what can be entered into a particular cell in your worksheet? Here are three separate ways you can ...

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}] (all 7 characters, in the sequence shown) 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 2 + 2?

2019-08-31 19:28:00

Donald Ashton

The macro for Unicode characters will not work correctly as defined because the underlying assumptions are incorrect.

1. Unicode values are not 16-bit. There are too many Unicode characters to be limited this way.
2. The allocation of the first 127 characters in Unicode corresponds to the 7-bit ASCII character allocation is only applicable to the English alphabet.
3. The characters imported may not be encoded as the correct ASCII values if a different code page has been used.


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