Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Getting Rid of Alphabetic Characters.

Getting Rid of Alphabetic Characters

Written by Allen Wyatt (last updated May 20, 2023)
This tip applies to Excel 97, 2000, 2002, and 2003


Bryan has a worksheet that has a lot of cells that have some alphabetic characters in them. He is looking for a way to get rid of only those alphabetic characters, no matter where they appear in the cell. For instance, if the cell contains "ABC123," Bryan wants to get rid of "ABC" and have just "123" remaining. Similarly, "A3B2C1" should become "321" and "#45P*%" should become "#45*%".

The only way to approach this problem is through the use of macros. If you want to simply strip out the characters, in place, then you can do so by selecting the cells you want to affect and then running a macro that examines each cell and deletes the offending characters. There are many ways you could do this; the following macro is a straightforward approach.

Sub CleanText1()
    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 = Asc(strCheckChar)
            Select Case intCheckChar
                Case 65 To 90      'upper case chars
                    'Do nothing
                Case 97 To 122     'lower case chars
                    'Do nothing
                Case 128 To 151    'special language chars
                    'Do nothing
                Case 153 To 154    'special language chars
                    'Do nothing
                Case 159 To 165    'special language chars
                    'Do nothing
                Case Else
                    strClean = strClean & strCheckChar
            End Select
        Next intChar
        rngCell.Value = strClean
    Next rngCell
End Sub

The nice thing about this approach to stripping out the characters is that you can easily get rid of other characters by simply modifying what is checked (and what actions are taken) in the Select Case structure.

If you don't want to modify the original cells, a good approach is to put together a user-defined function that will return a "clean" version of a string. This can be achieved by making a few modifications to the previous macro.

Function CleanText2(ByVal sRaw As String) As String
    Dim intChar As Integer
    Dim strCheckString As String
    Dim strCheckChar As String
    Dim intCheckChar As Integer
    Dim strClean As String

    Application.Volatile
    strClean = ""
    For intChar = 1 To Len(sRaw)
        strCheckChar = Mid(sRaw, intChar, 1)
        intCheckChar = Asc(strCheckChar)
        Select Case intCheckChar
            Case 65 To 90      'upper case chars
                'Do nothing
            Case 97 To 122     'lower case chars
                'Do nothing
            Case 128 To 151    'special language chars
                'Do nothing
            Case 153 To 154    'special language chars
                'Do nothing
            Case 159 To 165    'special language chars
                'Do nothing
            Case Else
                strClean = strClean & strCheckChar
        End Select
    Next intChar
    CleanText2 = strClean
End Function

In order to use the function, you could put a formula such as the following in a cell:

=CleanText2(A1)

The result is that the formula returns a "clean" version of whatever is in cell A1 without disturbing the contents of cell A1.

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 (3219) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Getting Rid of Alphabetic 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

Making Squares

Cells in a worksheet defined by the intersection of rows and columns. If you adjust row height and column width just ...

Discover More

Viewing Your Custom Styles

If you develop a set of preferred styles, you may want to use those styles with a document you receive from someone else. ...

Discover More

Ensuring Rows and Columns are Empty

Before you go about deleting rows and columns helter-skelter, it is a good idea to determine if there is anything in the ...

Discover More

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!

More ExcelTips (menu)

Deleting a Macro

Don't need that old macro any more? Here's how to get rid of it so that it is no longer a part of your workbook.

Discover More

Automatically Opening Macro Workbooks when Using a Shortcut Key

Click a button on a toolbar and Excel will go so far as to open a another workbook in order to run a macro associated ...

Discover More

Deriving the Worksheet Name

Excel doesn't provide an easy way to grab the worksheet name for use within a worksheet. Here are some ideas on ways you ...

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 + 8?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.