Answers / Question 22

Leading zeros disappear from ZIP codes and IDs (Excel + Google Sheets): stop it and get them back

Why Excel and Google Sheets turn 02108 into 2108 when you type, paste or open a CSV, how to stop it for good, and tested formulas that put the zeros back on codes that already lost them.

The problem

You type or paste a ZIP code like 02108, a product code like 00127 or an employee ID like 004521, and the spreadsheet shows 2108, 127, 4521. Opening a CSV export does the same to a whole column. Then lookups against the original codes start returning #N/A, and files you send on to other systems get rejected.

Inspired by a public question on Stack Overflow. The problem above is described in our own words and the solution below is ours.

Why it happens

Both apps read anything that looks like a number as a number, and a number has no leading zeros: 02108 and 2108 are the same value. The zeros aren't hidden, they're never stored. That's why changing the format to Text afterwards doesn't bring them back, and why opening the CSV in a text editor shows the zeros are still in the file. Very long numbers have a second problem: both apps keep only 15 significant digits, so a 16- to 20-digit code gets its last digits replaced with zeros and may display as 1.23457E+18.

Codes like ZIP codes, SKUs, account numbers and barcodes aren't quantities, so the fix is to store them as text.

Stop it before it happens

Already lost? Put the zeros back with a formula

If every code should have the same length, you can rebuild the zeros, because you know how many are missing. With the damaged codes in column A, put this in B2 and fill down (5 digits for US ZIP codes; change the zeros to match your length):

Pad to 5 digits (works whether A2 is a number or text)Formula
=TEXT(A2,"00000")
Same result without TEXT, and it never roundsFormula
=RIGHT("00000"&A2,5)
Any length: 6-digit IDsFormula
=TEXT(A2,REPT("0",6))
ZIP+4 stored as one number (8 or 9 digits)Formula
=TEXT(A2,"00000-0000")
Google Sheets: the whole column at once, blanks stay blankFormula
=ARRAYFORMULA(IF(A2:A="","",TEXT(A2:A,"00000")))

The results are text, so they keep their zeros when you export. To replace the original column, copy column B and paste it over A as values only, after formatting A as Plain text / Text.

How many codes in A2:A500 are shorter than 5 characters (lost zeros)Formula
=SUMPRODUCT((A2:A500<>"")*(LEN(A2:A500)<5))

Only need it to look right? A custom number format of 00000 (Excel: Ctrl+1 → Custom; Sheets: Format → Number → Custom number format) displays 2108 as 02108 while the cell stays a number. That's fine for printing, but lookups and many exports still see 2108.

If the codes have different lengths (say 007 and 0042 in the same column), there's no way to tell how many zeros each one lost. Re-import the original file with the column set to Text as shown above. The same goes for long numbers that lost digits after the 15th: the digits are gone, so you have to re-import them as text.

Lookups between text codes and number codes

A lookup only matches like with like: the text 02108 never equals the number 2108, so VLOOKUP, XLOOKUP and MATCH return #N/A. Convert the value you're looking up so it matches the table:

Table has text codes, your value lost its zerosFormula
=VLOOKUP(TEXT(A2,"00000"),Codes!A:B,2,FALSE)
Table has plain numbers, your value is text with zerosFormula
=VLOOKUP(VALUE(A2),Codes!A:B,2,FALSE)

Better still, fix both columns to text once, so every lookup just works. More invisible lookup mismatches are covered in VLOOKUP says #N/A but the value is there.

Good to know

Formulas follow documented Google Sheets / Excel behavior; they aren't machine-tested, so try them on a copy first. Always try new automations on a copy of your sheet first.