Excel is a very powerful program, but sometimes it doesn’t act the way we wish. A case in point is importing a CSV file with zip codes. If you’re not careful how you open your file and apply different formats, you could be in for some surprises. Excel may drop your leading zeros. The same problem can happen with part or order numbers.
Many companies have database systems that have contact data or sales data. It’s not uncommon to extract records and import them into another system. One common way of doing that is with CSV (comma separated values) files and Microsoft Excel.
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 cleanup work. If she were to look at her original file, she would see the zip codes had the leading zeros.
I’ve opened a sample file in Microsoft’s Visual Studio Code editor. All looks good.
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 so it’s easy to see the problem. However, if you have a large file that is not sorted on this column, 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 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 system she imported to doesn’t do field validation to find the issue. The end 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.
- Click either Zip Code or Zip Code + 4.
- Click OK. Your zip codes will now show the leading zeros.
- Save your file as .XLSX.
warning If you save the file in the same CSV format, the leading zeros would again drop even though it looks correct in Excel. 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.
- Open 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.
- Click the Disk icon in your Quick Access toolbar to save the file.
Your file will open 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.
Solution 3: The CSV File Dropped the Zeros
It could be that you saved the file from Excel and your CSV file no longer has the leading zeros. No worries, we can fix this problem.
- 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)
=text(E2, “00000”)and press Enter.
- 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
info The formula above will take the contents of cell E2, our bad zip code, and convert it to a text format. And it will prepend with as many zeros needed to get to 5 digits.
- Open your CSV files within Microsoft Excel so that the Text Import Widget opens.
- Change the Column Data Format to “text”.
- Use the =text formula to create new values based on bad ones.
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.
Disclaimer: Images from Amazon Product Advertising API. This site may receive an affiliate commission on these products. Updated: 2020-02-18