Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Sorting ZIP Codes.

Sorting ZIP Codes

by Allen Wyatt
(last updated September 1, 2016)

12

ZIP Codes come in two flavors: five-digit codes and nine-digit codes. If you type a column of ZIP Codes into Excel—some five-digits and some nine—you may wonder why they don't sort correctly.

The reason is that Excel recognizes the five-digit codes as numeric entries, and the nine-digit codes as text entries. (It is the dash in the middle of the nine-digit codes that makes Excel treat the entry as text.) The problem with sorting the numbers is that Excel treats numeric and text values differently when sorting.

The solution (without resorting to formulas and additional columns for sorting) is to make sure that you force Excel to treat all your entries as text. You can do this by formatting a column as text before putting in your first ZIP Code. Simply follow these steps:

  1. Select the column (or cells) you want Excel to treat as text.
  2. Display the Home tab of the ribbon.
  3. Click the small icon at the lower-right corner of the Numbers group. Excel displays the Format Cells dialog box.
  4. Make sure the Number tab is selected. (See Figure 1.)
  5. Figure 1. The Number tab of the Format Cells dialog box.

  6. In the list of available formats, choose Text.
  7. Click on OK.

This formats the cells as text, and you can then sort the ZIP Codes correctly. Don't be misled here, however: You must make the format change before you enter values in the cells. If you format the cells as Text after you enter information, they still won't sort properly. You can do the following, however:

  1. Format the cells as Text.
  2. Select a cell that has a five-digit ZIP Code in it.
  3. Press F2. This informs Excel you want to edit the cell.
  4. Immediately press Enter. This tells Excel you are done editing the cell.
  5. Repeat steps 2 through 4 for each cell that contains a five-digit ZIP Code.

Once this process is done, you can sort the cells, as desired.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12489) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Sorting ZIP Codes.

Author Bio

Allen Wyatt

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. ...

MORE FROM ALLEN

Backing Up Your Customized Toolbars

After customizing your Excel toolbars, it is a good idea to make a backup of the file that contains the information. Here's ...

Discover More

Finding Quoted Text in VBA

Macros are created for all sorts of purposes in creating, editing, and processing documents. You might want to use a macro to ...

Discover More

Easily Spotting Found Text

The color used by Word to draw your attention to "found text" when using Find and Replace can vary, based on a number of ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Sorting with Graphics

If you use graphics in a worksheet that are associated with certain cells (perhaps images of parts or icons for worksheet ...

Discover More

Sorting Data on Protected Worksheets

Protect a worksheet and you limit exactly what can be done with the data in the worksheet. One of the things that could be ...

Discover More

Incorrect Links after Sorting Hyperlinks

When you sort your data you should always check to see if the sort was done correctly. What if sorting messes up hyperlinks ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is nine minus 5?

2016-09-01 16:11:25

BCL

Cells may be formatted as ZIP codes from the git-go:

Format cells
Number
Special
ZIP Code _or_ ZIP Code + 4

Based on a quick test, these sort correctly, but only if you stick with ZIP or ZIP+4. If you try to mix the two by adding a 4-digit suffix to the ZIP format, the sort doesn't work. If you try to enter just 5 digits in the ZIP+4 format, the ZIP code fills from the right.


2016-09-01 16:05:47

Bigjaker

Craig that is a great tip.


2016-09-01 12:58:00

Craig

Long time ago I learned to format all numbers that will not be used in calculations as text when starting to design databases in MS Access; so it includes telephone numbers, social security numbers, part numbers, etc. If it will not be used in calculating another value then there is no reason to treat it as a number.


2016-09-01 10:35:29

Paul S.

If you have many of them, a quick way to convert is to add a helper column. If the zip codes are in column K starting in row 2 and the next empty column is R, then in cell R2, enter the formula
=k2&""
This will convert numbers to text. Copy the formula all the way down. Then copy column R and paste as values only onto column K.


2013-11-18 08:42:01

Glenn Case

Bruc:

The macro will work on multiple entires. Assime you have a column of 10000 zip codes. Just select the entire column & run the macro.


2013-11-15 09:31:39

Bruc

This works for single entry situations but what about when down loading from a database to excel or csv format? I am often working with thousands of records and cannot do each field individually.


2013-02-28 10:57:30

Gary

I learned long ago, you always want to use the text format with zip codes. In many north east states, the zip code begins with 0 which will be truncated if the format is General or Number.


2013-02-27 14:18:22

Glenn Case

Don: Thanks for the hint. It works great!

Jack: The macro will work on a block of cells; just select the block first before executing the macro.

The entire macro, plus Don's line, is as follows:

Sub ConvToTxt()

Dim txt As String
Dim cl As Range

For Each cl In Selection
ActiveCell.NumberFormat = "@"
txt = cl.Value
ActiveCell.FormulaR1C1 = txt
ActiveCell.Offset(1, 0).Select
Next cl
End Sub

Ken: Thanks also for your suggestion to use Text-to-Columns.

---Glenn


2013-02-25 18:21:53

Ken

Another quick and easy way to mass convert all the cells to TEXT is to select the range of cells you want converted, then use the 'text to columns' function found in the data tab.
I suggest to use the fixed width method and ensure that you set the column break wide enough to cover the longest zip code and then set the column format for that to be TEXT. When you click on the FINISH button excel will convert those selected cells to TEXT format.


2013-02-25 16:47:46

Jack

Steps 3,4,and 5 in the proposed solution need to be done for each individual cell.

Is there a way to do those steps for a block of cells?


2013-02-25 10:31:00

Don

Glenn

The piece that is missing is changing the formatting of the cell to Text. Here is the line (editted for quick insert to your

ActiveCell.NumberFormat = "@"


2013-02-25 09:43:18

Glenn Case

So any ideas on how to format existing entries as text in bulk or via a macro?

I tried copying and pasting such entries into pre-formatted cells using Paste Special/Values. No luck. I also tried the following macro:

Sub ConvToTxt()

Dim txt As String
Dim cl As Range

For Each cl In Selection
txt = cl.Value
ActiveCell.FormulaR1C1 = txt
ActiveCell.Offset(1, 0).Select
Next cl
End Sub

Also no luck. Any thoughts?

---Glenn


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.