Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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: Adding Leading Zeroes to ZIP Codes.

Adding Leading Zeroes to ZIP Codes

Written by Allen Wyatt (last updated September 12, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


2

When you import ZIP Codes from a text file into an Excel workbook, it is not uncommon for Excel to translate the values as numbers rather than as ZIP Codes. This results in leading zeroes being dropped from the ZIP Codes, which can obviously cause problems later when using the data for its intended purpose.

One solution, of course, is to simply change the display format used for ZIP Code cells. (Excel provides a special format for ZIP Codes, available through the Number tab of the Format Cells dialog box.) This may work for the display, but the underlying data is still missing the leading zeroes.

If you want the underlying data to actually include the leading zeroes, it is best to use a macro that goes through and adds leading zeroes to the information in a cell. The following macro does just that:

Sub MakeZIPText()
    Dim ThisCell As Range

    Application.ScreenUpdating = False
    'Make sure format is text
    Selection.NumberFormat = "@"
    For Each ThisCell In Selection
        'Strip the leading apostrophe, if any
        If Left(ThisCell, 1) = "'" Then
            ThisCell = Mid(ThisCell, 2, 99)
        End If
        'It's a 5-digit ZIP Code
        If Len(ThisCell) <= 5 Then
            ThisCell = "'" & Right("00000" & ThisCell, 5)
        Else
            ThisCell = "'" & Right("00000" & ThisCell, 10)
        End If
    Next ThisCell
    Application.ScreenUpdating = True
End Sub

To use the macro, simply select the range of cells containing the ZIP Codes, then run the macro. The macro actually changes the cell contents—no longer will the cells contain numeric values (the cause of the original problem), but they will contain text values. This allows the leading zeroes to appear at the beginning of the ZIP Codes.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9663) 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: Adding Leading Zeroes to 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

Unhiding Multiple Worksheets

You can hide a bunch of worksheets at the same time, but Excel makes it impossible to unhide a bunch at once. You can, ...

Discover More

Displaying Negative Percentages in Red

Excel includes quite a few different formats you can use for the information in a worksheet. One format that isn't as ...

Discover More

Functioning Check Boxes in a Protected Worksheet

Want to protect a worksheet but maintain the ability to make changes to the check boxes you add to the worksheet? Here is ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Calculating Time Differences between Two Machines

Want to know how much of a time difference there is between your machine and a different machine? This tip provides some ...

Discover More

Using InputBox to Get Data

Need your macro to get some input from a user? The standard way to do this is with the InputBox function, described in ...

Discover More

Extracting Proper Words

If you've got a list of potential words, and you want to know which of those potential words are real, you'll appreciate ...

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}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. 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 two more than 7?

2020-12-24 11:50:55

Willy Vanhaelen

This tip's macro can be simplified a bit:

- "Strip the leading apostrophe" in the macro does nothing since Left(ThisCell, 1) = "'" doesn't detect the apostrophe and simply returns the first digit of the number because the leading apostrophe in Excel looks like an apostrophe but it isn't: it is a special character.

- Preceding the 5 or 10 digit zip code with an apostrophe isn't necessary because the cells are formatted as text.

- Although it does no harm, suppressing the screen updating isn't really necessary for a rather simple task like this.

So this 5 lines macro does the job as well:

Sub MakeZIPText()
Dim Cell As Range
Selection.NumberFormat = "@"
For Each Cell In Selection
Cell = Right("0000" & Cell, IIf(Len(Cell) > 5, 10, 5))
Next Cell
End Sub


2020-09-13 03:21:27

Vicharak

Just wanted to confirm whether using text function in a helper column will give the same results..??

Like

=Text(A2,"00000")

0 can be repeated to reflect no. of total digits we need in the value including leading zeros..

In above formula 1234 will be changed to 01234 in the corresponding cell of helper column..


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.