Solved

Format phone number

Posted on 2014-07-21
1
475 Views
Last Modified: 2014-07-21
I'm trying to use some code to format a phone number column and fax number column that contain both US numbers and international numbers.

Here is what I'm trying:

Function cleanPhoneNumber(thisNumber As String) As String
    ' this function aspires to clean any phone number format
    ' to standard format (+9999) 999-999-9999 or 999-999-9999
    ' works with almost all phone number formats stored in text

Dim retNumber As String

For i = 1 To Len(thisNumber)
    If Asc(Mid(thisNumber, i, 1)) >= Asc("0") And Asc(Mid(thisNumber, i, 1)) <= Asc("9") Then
        retNumber = retNumber + Mid(thisNumber, i, 1)
    End If
Next
If Len(retNumber) > 10 Then
    ' format for country code as well
    cleanPhoneNumber = Format(retNumber, "(+#) (000) 000-0000")
Else
    cleanPhoneNumber = Format(retNumber, "(+1) (000) 000-0000")
End If
End Function

Open in new window


The function doesn't even show up when I click on macro to run it?  

Any help is appreciated.
Thanks.
0
Comment
Question by:-Dman100-
1 Comment
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40210668
This macro is a user-defined function, so it won't run like a regular macro subroutine.

Instead, in Excel, you'd enter this function as any other Excel function, like so:
=cleanPhoneNumber("17133456789")

The result would look like:
(+1) (713) 345-6789

You can also enter a cell reference in between the parenthesis:
=cleanPhoneNumber(B2)


Regards,
-Glenn
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Calculation 4 55
Need Help with Mapping ONLY 9 26
Turn date into age 17 33
Is there a way to link Access 2003 app to an xlsx sheet? 6 24
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question