Clean up List of Phone Numbers

jleasure457

New Member
Joined
Dec 27, 2018
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello, I am working on a formula to clean up a list of phone numbers with very inconsistent formats to all include just the 3 digit area code and following 7 digits. I have a formula that removes all spaces and non-numeric characters, but I can't figure out what I need to add that would remove extensions at the end of the phone number or country codes at the beginning.. I am using: =TEXTJOIN("",TRUE, IFERROR(MID(A2, ROW(INDIRECT("1:100")),1)+0,"")) , and this is the result. If this could be better accomplished with a macro, that would be acceptable as well. Any help would be appreciated!

Original Phone NumberNumber after Formula Applied
(999) 832-7870 (Mobile)9998327870
999-995-14559999951455
1-999-525-704519995257045
(999) 864-3091 ext. 3137 (Direct)99986430913137
(999) 526-3058 (Direct)9995263058
(999) 549-8913 (HQ)9995498913
+1 (999) 774-604119997746041
999-434-7736, ext 315899943477363158
(999) 422-8579 x902899942285799028
+44 (999) 774-6041449997746041
+339997746041339997746041
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
**snipp**
 
Last edited:
Upvote 0
what is your expected output?
 
Upvote 0
what is your expected output?
Hi @JEC . Thanks for the reply. I would like it to only include the 3 digit area code and 7 digits following. I've added a column below and indicated the cells in bold which I'm not receiving the expected output.
Original Phone NumberNumber after Formula AppliedExpected Output
(999) 832-7870 (Mobile)99983278709998327870
999-995-145599999514559999951455
1-999-525-7045199952570459995257045
(999) 864-3091 ext. 3137 (Direct)999864309131379998643091
(999) 526-3058 (Direct)99952630589995263058
(999) 549-8913 (HQ)99954989139995498913
+1 (999) 774-6041199977460419997746041
999-434-7736, ext 3158999434773631589994347736
(999) 422-8579 x9028999422857990289994228579
+44 (999) 774-60414499977460419997746041
+3399977460413399977460419997746041
 
Upvote 0
Here a solution with a UDF. Place this behind a new module

VBA Code:
Function jec(c As String) As String
  With CreateObject("vbscript.regexp")
    .Pattern = "(?:.*?)(999)(\))?( |-|)(.{7,8})(.*)"
    jec = Replace(.Replace(c, "$1$4"), "-", "")
 End With
End Function

Then you can use it like below

Book1
AB
1Original Phone NumberUDF
2(999) 832-7870 (Mobile)9998327870
3999-995-14559999951455
41-999-525-70459995257045
5(999) 864-3091 ext. 3137 (Direct)9998643091
6(999) 526-3058 (Direct)9995263058
7(999) 549-8913 (HQ)9995498913
8+1 (999) 774-60419997746041
9999-434-7736, ext 31589994347736
10(999) 422-8579 x90289994228579
11+44 (999) 774-60419997746041
12+3399977460419997746041
Sheet2
Cell Formulas
RangeFormula
B2:B12B2=jec(A2)


Or with an Excel formula

Excel Formula:
=LET(t,MID(A3,SEARCH("999",A2),13),TEXTJOIN("",,IFERROR(--MID(t,SEQUENCE(LEN(t)),1),"")))
 
Last edited:
Upvote 0
Thanks @JEC - the VBA script works well for the dataset provided, but I failed to clarify that my actual data set uses a variety of USA area codes. So, they will not consistently have utilize "999" as the first three numbers. Do you have any other suggestions?
 
Upvote 0
How about this?

VBA Code:
Function jec(c As String) As String
  With CreateObject("vbscript.regexp")
    .Pattern = "(?:.*?)(\+\d+)?(\d{3})(\))?( |-|)(.{7,8})(.*)"
    jec = Replace(.Replace(c, "$2$5"), "-", "")
 End With
End Function
 
Upvote 0
With Power Query
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Custom1 = Table.TransformColumns(Source,
     {{"Original Phone Number", each Text.Replace(Text.Replace(_, "(", ""), ")", ""),"-",""}}),
    #"Added Custom" = Table.AddColumn(Custom1, "Custom", each List.Accumulate(

    List.Numbers(0, Table.RowCount(Corrections)),
    [Original Phone Number],
    (A,B) =>
    Text.Replace(A,Corrections[Find]{B}, Corrections[Replace]{B}))),
    #"Replaced Value" = Table.ReplaceValue(#"Added Custom"," ","",Replacer.ReplaceText,{"Custom"}),
    #"Split Column by Character Transition" = Table.SplitColumn(#"Replaced Value", "Custom", Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c)), {"Custom.1", "Custom.2", "Custom.3"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Split Column by Character Transition",{"Custom.1"})
in
    #"Removed Other Columns"

Book2
ABC
1Original Phone NumberCustom.1
2(999) 832-7870 (Mobile)9998327870
3999-995-14559999951455
41-999-525-704519995257045
5(999) 864-3091 ext. 3137 (Direct)9998643091
6(999) 526-3058 (Direct)9995263058
7(999) 549-8913 (HQ)9995498913
8+1 (999) 774-604119997746041
9999-434-7736, ext 31589994347736
10(999) 422-8579 x90289994228579
11+44 (999) 774-6041449997746041
12339997746041339997746041
Sheet1
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top