Written by Allen Wyatt (last updated January 11, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Steve has phone numbers in column A, such as 3035551212, and would like to be able to look at the first three digits (the area code) and return in column B, the state with which that area code is associated. He wonders about the best way to accomplish this.
Excel has many functions that make life easier when you are trying to manipulate data. In this case, using the VLOOKUP function to match the area code to the corresponding state is simple.
Before applying a function to retrieve the information you want, you need to create a simple data table that contains the data you want retrieved. In this table, you need to have the area codes and states in their own columns side-by-side, within your worksheet, sorted by area code. For instance, you might put the area codes in column F and the states for those area codes in column G. The area codes and states can be found on a number of websites, or you can create your own table if you prefer.
Once you've got the data into the two columns, select those columns and create a name for the selected range. (How you create a named range has been covered in other issues of ExcelTips.) For example, you might name the range something like StateCodes. This naming, while not strictly necessary, makes using the lookup formula easier.
Assuming that the phone number is in cell A1 and that you would like the state name returned in the column next to the phone number, in cell B1 you would enter:
=VLOOKUP(VALUE(LEFT(A1,3)),StateCodes,2,FALSE)
The VALUE and LEFT functions are used to pull just the first three characters from the phone number. This is then used in the VLOOKUP formula to find the area code in the StateCodes table. Excel returns the name of the state that corresponds with the area code.
Another way that you can pull out the area code (which is essential for the lookup) is to use the FLOOR function, as shown here:
=VLOOKUP(FLOOR(A1/10000000,1),StateCodes,2,FALSE)
Note that this approach requires that the phone number be stored as a number, so it can be divided by 10,000,000.
The approaches discussed here work great, provided that your phone numbers are always in the specified format (3035551212). If your phone numbers have a different format—perhaps one that uses parentheses and dashes—then the formula won't work and will need to be adjusted to look at where the area code really is in the phone number. If you have phone numbers that are not in a single format, then all bets are off and the task of doing the lookup becomes much, much harder.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8065) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Determining a State from an Area Code.
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!
When you discover that there is an invalid reference in a workbook, you can have a bear of a time tracking down the ...
Discover MoreSumming data is a common need in Excel. Summing lots of data based on a condition that needs to be met can be a bit more ...
Discover MoreIf a series of cells contain the amount of money won by individuals, you may want to count the number of individuals who ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-01-11 20:30:12
SuperJames
The answer to the given problem is great, of course. It's instructive on VLOOKUP, FLOOR, etc.
I only question the wisdom of the problem: Since cell phones, this is tricky business. I know many, many people whose area code does NOT match the state they live in. Even if this were for a form that's being inputted, I would question preemptively placing the state in the form even if the user could change it--just not worth the chance that it'd be overlooked.
I'm sure it's possible to have an unusual need for this particular LOOKUP, but . . . it's not like the '70s.
2018-02-19 05:44:20
David Robinson
In the UK we have area codes that have a differing number of digits, most have 4 or 5 digits, but there are a handful with 3 or 6 digits too. In this situation I would start by looking up the leftmost 3 digits, and use error trapping to then look for 4 digits, then 5, then 6. I favour INDEX/MATCH over VLOOKUP, so my syntax would go... (and I hope my indenting shows correctly!) ...
=INDEX($E:$E, IFERROR(MATCH(LEFT(A2,3),$D:$D,0),
IFERROR(MATCH(LEFT(A2,4),$D:$D,0),
IFERROR(MATCH(LEFT(A2,5),$D:$D,0),
MATCH(LEFT(A2,6),$D:$D,0)))))
... where my area codes are in column D and the area names in column e, and A2 has the phone number I'm processing.
In Britain we use 0 as our initial dialing digit, e.g. Leeds is 0113, so I'd have to consider the possibility of Excel stripping out that first zero if it's entered as a number. I would mitigate this by formatting columns as text. I would also use a helper column to add the initial zero if it's stored as a numeric, i.e.
=IF(ISNUMBER(A2),"0"&TEXT(A2,"#"),A2)
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 © 2023 Sharon Parq Associates, Inc.
Comments