Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Format phone number

Posted on 2014-07-21
1
Medium Priority
?
557 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

772 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