Last active
January 23, 2023 17:33
-
-
Save josephlei/759da17e042617cce469 to your computer and use it in GitHub Desktop.
VBA macro to clean and format phone numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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