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
185 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

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

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;…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

708 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

13 Experts available now in Live!

Get 1:1 Help Now