Solved

Format phone number

Posted on 2014-07-21
1
459 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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now