Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Format phone number

Posted on 2014-07-21
Medium Priority
537 Views
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
``````

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

Any help is appreciated.
Thanks.
0
Question by:-Dman100-
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
1 Comment

LVL 27

Accepted Solution

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

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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!
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
###### Suggested Courses
Course of the Month11 days, 11 hours left to enroll