How to keep leading zeros in ZIP codes in Excel

A CSV of guest addresses turned the Boston ZIP 02134 into the number 2134. The leading zero was never stored. A custom format of 00000 can paint that zero back on screen, and the cell still holds 2134. The next sort, the next file save to CSV, or a lookup against the text 02134 uses the number.

The guest list that keeps these codes is the wedding guest list. Maria Lopez is mailed as 02134.

TEXT puts the zero in the value

TEXT turns the number into text with a format code. "00000" means five digits, with zeros on the left if the number has fewer.

TEXT
=TEXT(B1,"00000")
=TEXT(B1,"00000")B2
The imported number
AB
1Imported2134
2TEXT02134
3Length of TEXT5
4Length of the number4
5Excel 201002134
A picture of a CSV import. B1 is the number 2134. TEXT with 00000 returns the text 02134. LEN of that text is 5. LEN of the number is 4, because Excel turns 2134 into the characters 2134 before it counts.
=TEXT(2134, "00000")
=LEN(TEXT(2134, "00000"))
=LEN(2134)
=RIGHT("00000"&TEXT(2134, "0"), 5)

Those results are 02134, 5, 4, and 02134. TEXT is in every current Excel. The result is text, so SUM ignores it. That is the right outcome for a ZIP column. Adding ZIP codes was never a total.

The last formula is the body of xl.pad.zip. Five zeros are concatenated on the left of the digits, and RIGHT keeps five characters. 2134 becomes 000002134, and the right five are 02134.

=xl.pad.zip(2134)

That returns 02134. The name needs Microsoft 365, Excel 2024, or Excel for the web. Excel 2021 returns #NAME?. The RIGHT formula is the same five characters in Excel 2010.

A code that is already the text 02134 stays 02134. Padding 00000 in front of a five-character string and taking the right five is the same string.

The format that only changes the paint

Select the column, choose Format Cells, Custom, and type 00000. The number 2134 then displays as 02134. The formula bar still shows 2134. LEN of that cell is 4. A VLOOKUP or XLOOKUP for the text 02134 does not match it.

Set the column to Text before you paste, if you still have the original file. After the zero is gone, TEXT or the RIGHT formula is what writes it back. Formatting the cell afterward does not.

What counts as a ZIP shape

xl.is.zip checks two shapes: five digits, or five digits, a hyphen, and four digits. It does not check the Postal Service file. A code can have the right shape and still be unassigned.

xl.is.zip
=xl.is.zip(B1)
=xl.is.zip(B1)B2
Shape check
AB
1Five digits02134
2ValidTRUE
3ZIP+402134-1234
4ValidTRUE
5Short7870
6ValidFALSE
A picture after the zero is back. 02134 and 02134-1234 pass. 7870 has four digits and fails. The check is the pattern. It does not look the code up with the Postal Service.
=xl.is.zip("02134")
=xl.is.zip("02134-1234")
=xl.is.zip("7870")
=xl.is.zip("78701 1234")

The first two are TRUE. 7870 is FALSE. A space instead of the hyphen is FALSE. The ZIP+4 on this sheet is text. You cannot store the hyphen in a numeric cell and then ask TEXT to invent it.

The same check against the imported number fails, because 2134 has four digits. Austin’s 78701 never lost a zero, and it passes once it is text.

xl.is.zip
=xl.is.zip(B1)
=xl.is.zip(B1)B2
Before the pad
AB
1Imported2134
2Valid as importedFALSE
3Padded02134
4Valid after padTRUE
5Austin78701
6ValidTRUE
A picture of the number 2134 handed to the shape check. Four digits fail. The padded text passes. Austin's 78701, which never needed a leading zero, passes as text.
=xl.is.zip(2134)
=xl.is.zip(TEXT(2134, "00000"))
=xl.is.zip("78701")

Those results are FALSE, TRUE, and TRUE. Pad first when the source was a number. The name uses VALUE on the characters, so a four-digit number is a four-digit string after Excel coerces it.

xl.pad.zip keeps five digits. Hand it a ZIP+4 that was stored as the number 21341234 and it keeps the last five of that digit string, which is not the ZIP. Split the five-digit part before you pad. On text that still has the hyphen, the five-digit part is the left five characters:

=LEFT("02134-1234", 5)
=xl.is.zip("02134-1234")

Those results are 02134 and TRUE.


Template for this article

Wedding Guest List

More from the blog