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
- Google Sheets, typing or pasting: select the column, then Format → Number → Plain text, and only then type or paste the codes.
- Excel, typing or pasting: select the column, press Ctrl+1, choose Text, then enter the codes. In Microsoft 365, File → Options → Data → Automatic data conversion has a box for Remove leading zeros and convert to a number. Untick it and Excel stops stripping zeros when you type, paste or open a CSV.
- One cell: start the entry with an apostrophe,
'02108. The apostrophe isn't shown or exported; it just tells the app “this is text”. - Opening a CSV in Excel: don't double-click it. Use Data → From Text/CSV, choose Transform Data, select the code column and set Data Type → Text (choose Replace current when asked, so the automatic number conversion is undone), then Close & Load.
- Importing a CSV into Sheets: use File → Import → Upload and set Convert text to numbers, dates, and formulas to No.
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):
=TEXT(A2,"00000")=RIGHT("00000"&A2,5)=TEXT(A2,REPT("0",6))=TEXT(A2,"00000-0000")=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.
=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:
=VLOOKUP(TEXT(A2,"00000"),Codes!A:B,2,FALSE)=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
- Saving as CSV from Sheets or Excel keeps the zeros on text cells. They're lost again when someone opens that CSV by double-clicking it in Excel, so send XLSX if the recipient uses Excel, or tell them to use From Text/CSV.
- Excel may show a small green triangle on numbers stored as text. For code columns that's expected; select the cells, click the warning icon and choose Ignore Error.
- Don't run SUM or other math on code columns. If a code column also needs sorting, sort it as text so 00127 comes before 01000.
- References: Microsoft's Keeping leading zeros and large numbers and Google's TEXT function help pages.
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.