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.
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
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:
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.
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!
It is possible to create macros that send out reports, via e-mail, from within Excel. Frank did this and ran into ...
Discover MoreWrite out a check and you need to include the digits for the amount of the check and the value of the check written out ...
Discover MoreYou can create macros that are automatically executed whenever certain events occur within a worksheet. This tip details ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments