Are you struggling with disappearing leading zeros in Excel? We’ve got some solutions that will keep those zeros right where they belong. Imagine the ease and accuracy you’ll experience when your zip codes or other data retain their correct formatting. In this tutorial, I’ll show you how to keep leading zeros in Excel.
Many companies have database systems that have contact data or sales data. It’s not uncommon to export records and import them into another system. One common way of doing that is with CSV (comma-separated values) files.
Visualizing the Problem
Here’s a scenario a friend encountered. She opened a CSV file from one database into Excel, where she needed to do some data cleanup work. If she were to look at her original file, she would see the postal codes had the leading zeros.
I’ve opened a sample file in Microsoft’s Visual Studio Code editor. All looks good and you can see the leading zeros.
However, once the file was opened in Microsoft Excel, the leading zeros disappeared. You don’t see them even if you highlight the cell and look in the formula bar.
In my example file, all the zip codes had leading zeros, making it easy to see the problem. However, if you have a large unsorted file, it’s easy to miss.
Once my friend was done editing in Excel, she saved the CSV file and imported it to another system. Unfortunately, saving the file dropped the leading zeros, as you can see in my code editor.
The reason this happened is Excel formatted these cells as “General.” General format cells have no specific number format. And by default, Excel omits leading zeros.
The problem was compounded because the CMS (content management system) she imported to doesn’t do field validation to find the issue. The result was various contact records had zip codes that were less than 5 characters.
As with many problems, there are multiple solutions, and I’ll provide 3 based on whether your final files need to be .XLSX or .CSV
Solution 1: Change Cell Format and Save File to .XLSX
This solution is the easiest if you plan to change the file format from .CSV to .XLSX.
- Open the problem file.
- Click the letter above your Zip Code column (e.g., “E”). This will select the whole column.
- Press Ctrl + 1. The Format Cells dialog opens.
- Click Special from the Category column.
- Click either Zip Code or Zip Code + 4.
- Click OK. Your zip codes will now show the leading zeros.
- Save your file as .XLSX.
If you save the file in the same CSV format, the leading zeros will again drop even though it looks correct in Excel. So the next time you open the CSV file, your problem is back.
Solution 2: Changing to Text Format
Sometimes you need to keep the original CSV format. Your database system may allow you to import CSV files, but not Excel files. If that’s the case, please follow these steps. You can enable Excel Text Import Wizard from your Options pane if you don’t see it.
- Open Microsft Excel first.
- Click Open from the left menu.
- Navigate to your .CSV file
- Open the problem file. The Text Import Wizard – Step 1 of 3 should appear.
- Click the Next > button.
- On Step 2 of 3, select Comma as your Delimiter.
- Click the Next > button.
- On Step 3, click your column with the zip codes.
- At the top of the dialog, change the Column Data Format to Text.
- Click the Finish button.
Your file will open in Excel. The difference is that you have a green marker in the top left corner. This marker indicates the cell is using the text format. The leading zeros have been preserved.
- Click the Disk icon in your Quick Access toolbar to save the file.
Solution 3: Add Leading Zeros with the Text Function
It could be that you saved the file from Excel and your CSV file no longer has the leading zeros. We can fix this problem with the TEXT function.
- Add a new column to your spreadsheet called “Fixed Zip Code.” (e.g., Column H)
- Click in the first cell underneath that new column header. (e.g., H2)
- Type
=text(E2, “00000”)
and press Enter.
The formula above will take the contents of cell E2, our bad zip code, and convert it to a text format. The formula will prepend with as many zeros as needed to get to 5 digits.
- Copy the text formula by dragging the fill handle down the column.
- Click the Disk icon to save your file.
- Open Excel again.
- Navigate to the same file.
- On Step 3 of the Text Wizard, click the original Zip Code column and choose “Do not import column (skip).“
- Click the corrected zip code column and change the Column data format to Text.
- Click the Finish button.
Key Takeaways
- Open your CSV files within Microsoft Excel so that the Text Import Wizard opens.
- Change the Column Data Format to “text.”
- Use the =text formula to create new values based on bad ones.
Practice Files
Below are two CSV files you can use to practice the solutions if you like. The first one contains full zip codes, whereas the second has the leading zeros removed. All the names are fictitious and were created with a random name generator. Play along with each and learn to keep leading zeros in Excel CSV files.