You are on page 1of 5

Clean up Incorrectly Formatted Phone Numbers using Microsoft Excel - ...

Posted on September 30th, 2008 in ideas , Learn Excel - 16 comments In our Utopian imaginations all the data would have been standardized and shareable across systems and people. But alas, the reality is totally different. We seldom get data in the format / way we desire it to be. In other words, the ingredients are all there, but for us to prepare the dinner, you must pre-process them. Often this pre-processing or cleaning up the data takes quite an amount of time it self leaving very little to do the actual work. That is when you can use excel’s powerful data cleaning techniques to handle the situations. One common problem with corporate data is incorrectly formatted phone numbers. Most of us are used to a standard 10 digit phone number format like 123-123-1234 or (123) 123 1234, but when you get that customer data, very few phone numbers in it are formatted like above. Instead you might see phone numbers like 1231231234, 12312 31234, (123)123-1234 etc. It is not really difficult to clean up the phone numbers if we know before hand how they are formatted. For eg. you can easily convert a phone number like 1231231234 to 123-123-1234 using excel text formatting functions like =TEXT(1231231234,"000-000-0000"). But it is a rare case in which we have control over the incoming format and quickly you will have to use a slew of format / text processing functions to clean up the data. To simplify the whole thing, I have written a small VBA UDF (User Defined Function) which you can add to your excel add-ins list and use to clean up virtually any phone number format to standard phone number. Function cleanPhoneNumber(thisNumber As String) As String ' this function aspires to clean any phone number format ' to standard format (+9999) 999-999-9999 or 999-999-9999 ' works with almost all phone number formats stored in text Dim retNumber As String For i = 1 To Len(thisNumber) If Asc(Mid(thisNumber, i, 1)) >= Asc("0") And Asc(Mid(thisNumber, i, 1)) <= Asc("9") Then retNumber = retNumber + Mid(thisNumber, i, 1) End If Next If Len(retNumber) > 10 Then ' format for country code as well cleanPhoneNumber = Format(retNumber, "(+#) 000-000-0000") Else cleanPhoneNumber = Format(retNumber, "000-000-0000") End If End Function

1 of 5

9/26/2011 8:11 PM

in those cases your code is suitable. for example since I deal with US 2 of 5 9/26/2011 8:11 PM ..Replace what:=”+”.ScreenUpdating = True End Sub 2) Chandoo October 1. Like this? Learn these other data cleaning / processing tips: Handling spelling mistakes in your data Splitting text using excel formulas Generating initials from names using excel Adding a range of cells using Concat() Comments 1) JP September 30. MatchCase:=True . MatchCase:=True . _ SearchOrder:=xlByColumns. Replacement:=”". and add a few conditions. LookAt:=xlPart.Clean up Incorrectly Formatted Phone Numbers using Microsoft Excel .. Replacement:=”". 3) & “-” & Right(cell. Replacement:=”". It scans the input text for any numeric ASCII codes and saves them to another text field. that is one reason for writing the above function. 3) JP October 1. _ SearchOrder:=xlByColumns. ‘ ‘ For each cell in Selection ‘ cell = “(” & Left(cell. LookAt:=xlPart. Replacement:=”". My code assumes that it’s only numbers and maybe a few odd characters: Sub Convert_Phone() Application.SpecialCells(xlConstants) . otherwise to (+9999) 999-999-9999 format (with country code). The above function is pretty straight forward and simple.Replace what:=”)”.Replace what:=Chr(32).NumberFormat = “(###) ###-####” ‘ 2.ScreenUpdating = False ‘ ‘ first highlight the section you want to work on ‘ With Selection. Once the scanning is complete the function will format the final number to 999-999-9999 format if the number has 10 or less digits. _ SearchOrder:=xlByColumns.Replace what:=Chr(160). _ SearchOrder:=xlByColumns. Replacement:=”".. Replacement:=”". _ SearchOrder:=xlByColumns. do a “virtual” format where you just make the cell *appear* to be a phone number. MatchCase:=True ... MatchCase:=True .Replace what:=”-”. most of the times we know how the incoming format is.Replace what:=”(“. 4) ‘ Next cell ‘ ‘ uncomment whichever one you want! ‘ ‘ Application. http://chandoo. LookAt:=xlPart.. LookAt:=xlPart. MatchCase:=True End With ‘ ‘ ‘ ‘ at this point you could do one of two things: 1. 4. MatchCase:=True . 2008 Here’s the code I use. We can actually insert the parentheses and dash in the appropriate place. LookAt:=xlPart. Selection.. _ SearchOrder:=xlByColumns. 3) & “) ” & Mid(cell. 2008 What I’ll probably do is convert your code to a routine that can be used on a selection (similar to what mine is now). 2008 LookAt:=xlPart. but I like yours better since it handles text. merge some of my code in. I wanted to comeup with something that can applied to almost all cases… As I mentioned in the article..

just save the file as “Excel add in” type (menu > save as. make sure you check it to install that add-in. it begins at this location For i = 1 To Len(thisNumber) 3 of 5 9/26/2011 8:11 PM . http://chandoo. click menu > insert > module 3. Now from tools > addins. using this method. copy paste the code from here to there 4. phone numbers only: If Len(retNumber) < 10 Then cleanPhoneNumber = “not valid” ‘ show error or If Len(retNumber) < 10 Then cleanPhoneNumber = retNumber ‘ just return the number 4) Sam October 31. 2008 @Sam: Thanks for the comments. What am I missing here? 7) Dan July 16. 2009 Ok – I tried following the above instructions and created the UDF and even added it to my “add-ins”. right click on the sheet name and select view code 2. I would love you forever! -Sam 5) Chandoo October 31. close it and return to your excel sheet 6.) So for example: Input: (999) 999 9999 Ext. remember. 2010 This was close to what I needed except it didn’t handle extensions. and file type). Let me know if you have any difficulties with this method. Now use the cleanPhoneNumber() in your cells like you would use sum(). This way. You will see a blank module (basically a place where you can write all your macros and udfs). If you need to use it in all the workbooks. If it finds the letter “x” anywhere beyond the 7th digit. so I expanded on your function below. 6) Nelson July 16. 123 Output: 999-999-9999 x123 Function cleanPhoneNumber(thisNumber As String) As String ‘ this function aspires to clean any phone number format ‘ to standard format (+9999) 999-999-9999 or 999-999-9999 ‘ works with almost all phone number formats stored in text Dim retNumber As String Dim ext As String Dim extStart As Integer extStart = 0 ‘ if an extension is present.... it’s not recognizing the name “cleanphonenumber” in the formula.Clean up Incorrectly Formatted Phone Numbers using Microsoft Excel . the cleanPhoneNumber() will only work in that particular workbook. if() When I step through the logic on the formula. Is there a way for me to use this code some other way? Has anyone already packaged it up as an add-in I can download? If so. in the resulting window. it uses the right-most location. Once the file is shown. (If “x” is found more than once. click browse and point to the saved excel addin file. Just follow these steps: 1.. just in case 7. Welcome You can install a vba user defined function (UDF) like this very simply. In your workbook. i’m seeing the following result: “#NAME?”. but in the cell where I am putting the formula. save the work book. save by pressing ctrl+s 5. you can use cleanPhoneNumber() in all the workbooks. it will assume the following numbers are an extension and separate it out.. 2008 This looks great! How can add this add-in to my Excel workbooks? I don’t know anything about VBA and don’t have VBE.

separate it out ext = Right(retNumber.. 1) End If Next ReplaceAll = retStr End Function 9) Chandoo August 4.. replaceWith As String) As String Dim retStr As String For i = 1 To Len(tStr) If Mid(tStr. “000-000-0000″) End If If extStart > 0 Then ‘ append the extension if present cleanPhoneNumber = cleanPhoneNumber + Format(ext... “(+#) 000-000-0000″) Else cleanPhoneNumber = Format( Function ReplaceAll(tStr As String. 2010 Interesting function. i. I wrote a simple replace all based on the same logic as the cleanPhoneNumber function. http://chandoo. If Asc(Mid(thisNumber. 1)) = “x” Then extStart = Len(retNumber) + 1 End If Next If extStart > 0 Then ‘ if an extention is present. When I try it. just use paste special > formats (keyboard shortcut – ALT+E then s and t ) 4 of 5 9/26/2011 8:11 PM . extStart – 1) End If If Len(retNumber) > 10 Then ‘ format for country code as well cleanPhoneNumber = Format(retNumber. hope you guys will enjoy this one in excel. Thank you for the function. 10) barty August 10. 1) = s2Replace Then retStr = retStr + replaceWith Else retStr = retStr + Mid(tStr. ” x#”) End If End Function 8) Deepak Pathak August 3. i. very good extensions. 2010 @Mohaaron… Welcome to chandoo.. How can I copy/paste the resulting value? I need to do this so I can copy/paste thousands of rows with the newly formatted phone number. 2010 HI I have been having the same problem as someone else above. 1)) >= Asc(“0″) And Asc(Mid(thisNumber. If you want to keep the formatting alone. Can any one help me? 11) Mohaaron September 24. it keeps telling me ambiguous name and it puts a “#NAME?” in the cell where the formula was. i. i. s2Replace As and thanks for comments. Len(retNumber) – extStart + 1) retNumber = Left(retNumber. 2010 When copy/pasting the newly formatted phone number the paste is pasting the function name instead of the phone number. Here goes. 12) Chandoo September 25. 1)) = 7 And LCase(Mid(thisNumber. 2010 @Dan and Deepak. Thank you for sharing.. i.Clean up Incorrectly Formatted Phone Numbers using Microsoft Excel .

13) Cannon November 3.. Do I need to add something to the code to make it change these formats too…? 14) Krobb Lattrell November 23. Thanks for all the help everyone! 15) Patricia June 15. If you want to use my function above to format the phone number.. A lot of them have brackets and I want it to be just straight 000-000-0000 or they just have spaces such as 000 000 0000.. TrackBack URI Leave a comment 5 of 5 9/26/2011 8:11 PM .Clean up Incorrectly Formatted Phone Numbers using Microsoft Excel . 2010 if you’re getting a #NAME error. 2011 Thanks for the code! You saved me a ton of time on my data cleansing efforts. 2011 Just wanted to say thank you! This worked like a charm! 16) Angela July 20. then assuming your first phone number is in A1. RSS feed for comments on this post. 2010 I copy and pasted the text but I don’t think it worked because the phone numbers are entered too many different ways in the document. you need to make sure you’ve followed all of the instructions -they do work! I forgot the Insert -> Module part for an hour or so before fixing that and was stuck on the #NAME error.. write =cleanPhoneNumber(a1) and drag it down to other cells..