Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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.
by Allen Wyatt
(last updated December 20, 2014)
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10565) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Getting Rid of 8-Bit ASCII Characters.
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!
Want to delete a bunch of rows in a worksheet based on the value in a certain cell of each row? There are a couple of ...Discover More
What do you do if a keypress you know worked correctly before all of a sudden stops working as you expect? This tip ...Discover More
Excel allows you to reference data in other workbooks by establishing links to that data. If you later want to get rid of ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.