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

Freezing a Table

Tired of Word changing the dimensions of table cells to accommodate what you place in those cells? You can instruct Word ...

Discover More

Merging Table Cells

Want to create cells that span multiple columns or multiple rows? You do this in Word by merging cells together. Here's ...

Discover More

Inserting and Copying Rows

Want an easy way to insert a new row in a worksheet and copy everything from the row above? (You end up with two ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Converting Strings to Numbers

When working with data in a macro, there are two broad categories you can manipulate: numbers and text. Sometimes you ...

Discover More

Replacing Commas with Periods

Want to replace all commas in a formatted number with periods, and vice-versa? There are a couple of approaches you can ...

Discover More

Executing a Macro Every 15 Minutes

Need to run a macro at a given interval? It's easy to do when you learn how to use the .OnTime method, described in this tip.

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

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.