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: Converting Phone Numbers.

Converting Phone Numbers

Written by Allen Wyatt (last updated August 2, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


3

We have all seen the ads on TV: "Call 1-800-GET THIS for your set of super-sharp knives." You may be faced with the need to convert phone numbers from the text version (as shown on the ads) to the numbers represented by that text. The following macro, DoPhone, will perform the conversion magic for you:

Sub DoPhone()
    Dim rngSrc As Range
    Dim lMax As Long, lCtr As Long
    Dim J As Integer
    Dim Phone As String, Digit As String

    Set rngSrc = ActiveSheet.Range(ActiveWindow.Selection.Address)
    lMax = rngSrc.Cells.Count

    For lCtr = 1 To lMax
        If Not rngSrc.Cells(lCtr).HasFormula Then
            Phone = rngSrc.Cells(lCtr).Value
            For J = 1 To Len(Phone)
                Digit = Ucase(Mid(Phone, J, 1))
                Select Case Digit
                    Case "A" To "P"
                        Digit = Chr((Asc(Digit) + 1) \ 3 + 28)
                    Case "Q"
                        Digit = "7"     'May want to change
                    Case "R" To "Y"
                        Digit = Chr(Asc(Digit) \ 3 + 28)
                    Case "Z"
                        Digit = "9"     'May want to change
                End Select
                Mid(Phone, J, 1) = Digit
            Next J
            rngSrc.Cells(lCtr).Value = Phone
        End If
    Next lCtr
End Sub

The DoPhone procedure tries to convert the information in any cell that does not contain a formula. All you need to do is select the cell (or cells) you want to convert, and then run the procedure. The result is that any text in the cells are converted to their digit equivalents on a phone. Thus, 598-Tips becomes 598-8477.

You should note one small peculiarity of DoPhone, and you may want to change it. Some phones recognize the letters Q and Z as the digits 7 and 9, respectively. Others simply leave these digits out, or they are converted to 0. DoPhone, as written here, converts these letters to 7 and 9. You can change the appropriate places in the Select Case structure, as desired, so they are changed to numbers according to your needs. (The appropriate places are commented in the listing.)

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 (2269) 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: Converting Phone Numbers.

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

Determining the Template Attached to a Document

If you've opened a document in Word, that document has a template attached to it. This tip looks at what those templates ...

Discover More

Specifying an Order for Page Printing

If a printout of your worksheet requires multiple pages, you may want to specify the order in which Excel prints those ...

Discover More

Changing Decimal Commas to Decimal Points

If you have a document that was produced in a country where decimal commas are used instead of decimal points, you may be ...

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)

Shortening ZIP Codes

US ZIP Codes can be of two varieties: five-digits or nine-digits. Here's how to convert longer ZIP Codes to the shorter ...

Discover More

Converting UNIX Date/Time Stamps

If you import information generated on a UNIX system, you may need to figure out how to change the date/time stamps to ...

Discover More

Converting PDF to Excel

Reports and other formal documents are often distributed in PDF format so that they can be read and printed on a variety ...

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 four minus 0?

2018-01-28 03:24:17

Rick Rothstein

Note the lone "False" on the fourth line up from the bottom of the code I just posted... that actually belongs at the end of the line above it (it got wrapped to the next line because the code line was too long for the field it was displayed in).


2018-01-28 03:19:52

Rick Rothstein

Here is another way to do it which should be a little bit faster, especially for a large selected range (which may also be non-contiguous)...

Sub DoPhone()
Dim X As Long
Const Letters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Const Numbers As String = "22233344455566677778889999"
Application.ScreenUpdating = False
For X = 1 To 26
Selection.Replace Mid(Letters, X, 1), Mid(Numbers, X, 1), xlPart, , False, , False, False
Next
Application.ScreenUpdating = True
End Sub


2018-01-27 10:31:40

Rick Rothstein

Here is another way to write this article's DoPhone macro which will also handle a non-contiguous selection (which this article's macro won't) in case that feature should be needed.

Sub DoPhone()
Dim X As Long, Txt As String, Cell As Range
Const Nums As String = "22233344455566677778889999"
For Each Cell In Selection
If Not Cell.HasFormula Then
Txt = UCase(Cell.Text)
For X = 1 To Len(Txt)
If Mid(Txt, X, 1) Like "[A-Z]" Then
Mid(Txt, X) = Mid(Nums, Asc(Mid(Txt, X, 1)) - 64, 1)
End If
Next
Cell.Value = Txt
End If
Next
End Sub


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.