Solved

Need VBA code to auto format phone numbers by removing all periods, spaces and dashes and then formatting it

Posted on 2014-10-28
6
190 Views
Last Modified: 2014-10-28
When I copy one of the below phone numbers into cell B7 on sheet1, I need it to resolve to (555) 555-5555

Examples
555-555-5555
(555)-555-5555
555 555 5555
555.555.5555
0
Comment
Question by:kbay808
  • 3
  • 3
6 Comments
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40408958
This EE question has two viable solutions for you.  zorvek (Kevin Jones) uses a subroutine that updates a highlighted selection; Patrick Matthews uses regular expressions in a user-defined function.

As a new user of regular expressions, I like Patrick's solution.  Here's the code without the comments included:
Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
     Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True)
     
    Dim RegX As Object
     
    Set RegX = CreateObject("VBScript.RegExp")
     With RegX
         .Pattern = PatternStr
         .Global = ReplaceAll
         .IgnoreCase = Not MatchCase
     End With
     
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
    Set RegX = Nothing     
End Function

Open in new window


Nothing but the numbers

-Glenn
0
 

Author Comment

by:kbay808
ID: 40408977
How do I run it for just cell B7 on sheet1?  Also, does this script go on sheet1 or on a separate module?
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40408994
This code goes in a regular VBA module.

You call it in Excel like a regular function.  Add this to an adjacent cell:
=RegExpReplace(B7,"\D","",TRUE)

If you actually want a number:
=VALUE(RegExpReplace(B7,"\D","",TRUE))

If you want to update/replace the original phone numbers, set up a temporary column with this formula, copy and replace the results with values (Paste Special... Values) and then paste over the original column.

-Glenn
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:kbay808
ID: 40409178
I’m sorry, but I must not have been clear.  I need to be able to paste the phone number into cell B7 and have the result also be in cell B7.  That is why I’m looking for a vba solution.  Otherwise I would of used the substitution function.
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40409401
If you want a VBA solution that isn't a UDF, then here is a subroutine also uses regular expressions to do this, but is specific to column B.
Option Explicit
Sub Fix_Phone_Nos()
    Dim rng As Range
    Dim cl As Object
    Dim RegX As Object
     
    Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = "\D"
        .Global = True
        .IgnoreCase = True
    End With
    
    Set rng = Range("B2:B" & Cells.SpecialCells(xlLastCell).Row)
    For Each cl In rng
        cl.Value = RegX.Replace(cl.Value, "")
    Next cl
End Sub

Open in new window


Regards,
-Glenn
0
 

Author Closing Comment

by:kbay808
ID: 40409771
Thank you very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

862 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

23 Experts available now in Live!

Get 1:1 Help Now