Skip to content

Instantly share code, notes, and snippets.

@josephlei
Last active January 23, 2023 17:33
Show Gist options
  • Save josephlei/759da17e042617cce469 to your computer and use it in GitHub Desktop.
Save josephlei/759da17e042617cce469 to your computer and use it in GitHub Desktop.
VBA macro to clean and format phone numbers
Sub PHONECLEAN()
Dim rngActiveRange As Excel.Range
Set rngActiveRange = Selection
rngActiveRange.Select
' REMOVE ALL NON-NUMERICAL CHARACTERS
Selection.Replace What:=".", Replacement:=""
Selection.Replace What:="-", Replacement:=""
Selection.Replace What:=" ", Replacement:=""
Selection.Replace What:="(", Replacement:=""
Selection.Replace What:=")", Replacement:=""
'DELETE LEADING 1 IF IT IS PRESENT
For Each x In rngActiveRange
If Len(x.Value) = 11 Then
x.Value = Right(x.Value, 10)
End If
Next
'FORMAT IT TO LOOK LIKE A PHONE NUMBER
Selection.NumberFormat = "[<=9999999]###-####;(###) ###-####"
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment