Ron has a worksheet that has a column of phone numbers. Some are seven-digit numbers, as in 123-4567, with the 727 area code assumed. Others have area codes included, as in (890) 123-4567. Ron wants to have every phone number in the latter format and wonders about the best way to detect the shorter phone numbers and add the missing (727) to those numbers.
The best way to go about this is going to depend on the nature of your data. You see, it is possible for the data to be text or numeric. This is possible because Excel provides a special cell format that allows numbers like 8015551212 to be displayed as (801) 555-1212. You can tell whether your data is numeric or text by comparing what you see in the cell with what is shown in the formula bar. (See Figure 1.)
Figure 1. Figuring out if a phone number is text or numeric.
If your data happens to be numeric, you can easily add the proper area code by using a formula such as this:
=IF(A1<=9999999,7270000000+A1, A1)
The result will add the proper area code, but you will still need to format the cell containing this formula with the special Phone Number format.
If the data is textual, then you can simply use a formula to check the length of the cell and then add the area code, as needed:
=IF(LEN(A1) = 8, "(727) " & A1, A1)
Thus, if the phone number is 555-1212 (8 characters long), then this formula returns (727) 555-1212. Of course, it is possible that the phone number may have some extra spaces around it; this often happens if phone numbers are pasted into a worksheet from a different source. In that case, just include the TRIM function in your formula:
=IF(LEN(TRIM(A1)) = 8, "(727) " & TRIM(A1), TRIM(A1))
There's another formula you may find very helpful for adding the area code, and it is shorter than those we've seen so far:
=RIGHT("(727) " & TRIM(A1), 14)
This formula adds the area code to all the cells and then just takes the rightmost 14 characters of the result.
Things become a bit stickier if your data has some phone numbers that are numeric and some that are textual. In that case, you can still put together a formula, but it becomes longer:
=IF(LEN(TRIM(A1)) > 9, TRIM(A1), IF(LEN(TRIM(A1)) = 8, "(724) " & TRIM(A1), IF(LEN(TRIM(A1)) = 7, 7240000000+A1, "Undetermined")))
Remember that this is a single formula. It keys off the length of whatever is in cell A1. If the length is over 9 characters, then the phone number is assumed correct. If the length is exactly 8, then the formula assumes it is a "short text" version of the phone number, such as 555-1212, and adds the area code to the beginning. If the length is exactly 7, then it is assumed to be a "short numeric" phone number and the proper number is added to it. If the phone number is not any of these, then the formula returns the text "Undetermined." After using this formula, you'll still need to format the resulting cells using the special Phone Number format.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13564) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
If you have a series of consecutive numbers in a column, you may want to know if it really is consecutive. (In other ...
Discover MoreAdding row numbers to a column of your worksheet is easy; you just need to use a formula to do it. Here's a quick look at ...
Discover MoreWhen working with names or a different series of words, you may need to pull the initial letters from each word in the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-10-28 17:13:07
Beth
This was extremely helpful, thanks! My list had area codes for everyone but those with my same area code. Using this formula took me about 5 minutes to fix hundreds of records with both phone and cell columns. =IF(LEN(TRIM(A1)) = 8, "(727) " & TRIM(A1), TRIM(A1))
2019-05-29 23:13:03
Andy
I can appreciate the work you put into this but it really makes sense to a beginner to intermediate level of understanding with excel. I tried all of the formulas without any luck. My suggestion would be to show some examples (with images) to really help the user with this formula.
2018-09-24 07:02:49
Tom Van Dam
since you really want to consider all of the cell contacts as text (since we aren't going to do any math) I would first format all of the fields as text, this way you could shorten the formula some and only have to deal with the text portion of the formula.
2018-09-22 22:57:38
Erik
This assumes all your friends have the same area code, which is highly unlikely these days. If they don't, the best way is probably just to add the missing area codes manually.
2018-09-22 08:32:02
Ray McAllister
If you go to the effort of detecting numeric o text phone number in order to make certain they all have area codes, you might as well convert all telephone numbers to numeric format. This would allow sorting the field numerically and adjust the custom display format.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments