Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company.
Learn more about Allen...
Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Determining a State from an Area Code.
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:
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:
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 (8063) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Determining a State from an Area Code.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!