Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. 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: Converting Phone Numbers.

Converting Phone Numbers

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


5

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 c As Range
    Dim J As Integer
    Dim Phone As String, Digit As String

    For Each c In Selection
        If Not c.HasFormula Then
            Phone = Ucase(c.Value)
            For J = 1 To Len(Phone)
                Digit = 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
            c.Value = Phone
        End If
    Next c
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 is 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 (11802) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel 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

Turning Off Worksheet Tabs

Look at the bottom of a worksheet and chances are you will see tabs for all the worksheets in the current workbook. Want ...

Discover More

Hiding a Hyperlink on a Printout

Hyperlinks can be real handy in a workbook, but you may not always want them visible when you send the workbook to the ...

Discover More

Automating Copying Macros

You can manually copy macros from one workbook to another, but what if you want to automate the copying process? Here's ...

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 Based on a Partial File Name

A common task for macros is to open and process a file you want imported into your workbook. If you need to identify the ...

Discover More

Default Worksheet when Opening

When opening a workbook, you may want to make sure that a particular worksheet is always displayed first. The only way to ...

Discover More

Finding the Last-Used Cell in a Macro

Ever wonder what the macro-oriented equivalent of pressing Ctrl+End is? Here's the code and some caveats on using it.

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 two more than 0?

2022-08-21 05:03:54

Willy Vanhaelen

@J.Woolley
Thanks for your appreciation. Take also a look at Rick Rothstein's alternatives in the <2007 version of this tip.


2022-08-16 15:22:57

J. Woolley

@Willy
I loved your macro, so I added the following function to My Excel Toolbox (with credit to you):
=PhoneTextToDigits(PhoneNumber, [AsNumeric])
It returns either formatted text (default) or a numeric value; the latter permits custom cell formatting or use of the TEXT function.
Here is an abreviated version:

Function PhoneTextToDigits(PhoneNumber As String, Optional AsNumeric As Boolean)
'see Willy Vanhaelen, https://excelribbon.tips.net/T011802_Converting_Phone_Numbers.html
    Dim sNum As String, sDig As String, n As Integer
    sNum = UCase(PhoneNumber)
    For n = 1 To Len(sNum)
        sDig = Mid(sNum, n, 1)
        If sDig Like "[A-Z]" Then
            Mid(sNum, n, 1) = Mid("22233344455566677778889999", (Asc(sDig) - 64), 1)
        ElseIf AsNumeric And sDig Like "[!0-9]" Then
            Mid(sNum, n, 1) = " "
        End If
    Next n
    If AsNumeric Then
        PhoneTextToDigits = CDec(Replace(sNum, " ", ""))
    Else
        PhoneTextToDigits = sNum
    End If
End Function

See https://sites.google.com/view/MyExcelToolbox/


2022-08-15 07:02:36

Willy Vanhaelen

@John Man
In vba Mid can be used in two ways:
1) the Mid statement: replaces a specified number of characters in a Variant (String) variable with characters from another string.
Syntax: Mid(stringvar, start, [ length ] ) = string
2) the Mid function: Returns a Variant (String) containing a specified number of characters from a string.
Syntax: Mid(string, start, [ length ])

The statement Mid(phone,J,1) replaces the character at position J in the phone string, only 1, with the result of
the function Mid("22233344455566677778889999", Asc(digit)-64,1).

"22233344455566677778889999" is the of string of digits to search from
The variable digit is the character to be replaced by the corresponding cipher.
Asc(digit)-64 returns the ascii character code from digit and subtracts 64 form it resulting in a numuber 1 tot 26.

So A,B and C will be replced by 2 ... W,X,Y and Z by 9.


2022-08-14 07:16:52

John Mann

@Willy. I'm a very raw VBA newbie, but I oftern try to follow along on these scripts. I'm having diffuculty understanding one line of your version.

The line I'm puzzled by reads "Mid(phone,J,1)=Mid("22233344455566677778889999", Asc(digit)-64,1)". I don't see how you pick off which digit to use from the long string following the equals sign.

Thanks for any explanation.


2022-06-04 12:41:55

Willy Vanhaelen

The macro in this trip is rather complicated. It also replaces each digit, hyphen and space with it own which is a waist of tilme.

Here is my version that is smaller and replaces only the letters making it faster:

Sub DoPhone2()
Dim cell As Range, digit As String, J As Integer, phone As String
For Each cell In Selection
   If Not cell.HasFormula Then
   phone = UCase(cell)
      For J = 1 To Len(phone)
         digit = Mid(phone, J, 1)
         If digit Like "[A-Z]" Then
            Mid(phone,J,1)=Mid("22233344455566677778889999", Asc(digit)-64,1)
         End If
      Next J
   cell = phone
   End If
   Next cell
End Sub


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.