Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and 2021. 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.
Written by Allen Wyatt (last updated April 30, 2025)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9663) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021. You can find a version of this tip for the older menu interface of Excel here: Adding Leading Zeroes to ZIP Codes.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
At times, you may want your macro to pause execution. This tip presents four different ways you can add a delay into your ...
Discover MoreThe VBA programming language provided with Excel allows you to create and modify text files quite easily. Here's how to ...
Discover MoreCopying worksheets (one or many) is easy to do manually. What is not well known is that it is even easy to make the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2025-05-01 13:39:14
Kerry
Re: leading zeros.
I think it is easier than a macro to add leading zeros to numbers.
Use a self-created custom number format such as 0000# for a five digit number using format>number.
2025-04-30 05:25:42
jamies
Basic premise -
a number is something you do arithmetic with !
As you would not be incrementing, decrementing, multiplying a zip code, but using it as a code -
then it should be held within Excel as a text string -
So- set the actual cell storage format to "Text"
Import codes as "Text"
and - re the leading ' that is often used by users to force Excel not to treat the value as a number -
Ideally check for, and remove the first of a double apostrophe if processing using VBA
And to force Excel to treat a value generated by a formula as text concatenate the value with a prefix of a double quotation as in something like
if(OR(value=0,len(value)=0,""&value,value)
or just
=""&cellid
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..
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 © 2025 Sharon Parq Associates, Inc.
Comments