Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Handling Leading Zeros in CSV Files.

Handling Leading Zeros in CSV Files

by Allen Wyatt
(last updated May 23, 2015)

4

John wrote concerning a problem with handling leading zeros in CSV files. He creates CSV files destined for import into an accounting program, but has problems with the CSV files if he needs to first reload the CSV into Excel to correct any mistakes in the file.

Actually, there are two things that need to be checked here. First, is Excel putting the leading zeros in the CSV file it initially creates? Second, is it maintaining the zeros in the CSV file when you reload it and then resave it? These are two separate issues.

You can check the first issue easily enough. All you need to do is rename the CSV file so it has a TXT extension, then you can load it into a text editor, such as Notepad. There you can examine the actual CSV file, as created by Excel, to make sure that everything is in the format you expect. If it is not—for instance, there are no leading zeros where you need them—then you need to be concerned with how Excel is creating the CSV file in the first place.

You need to check whether there are leading zeros in the original Excel information. If there are, and they are displayed, then you need to make sure that the column in which the data is contained is formatted as Text in the Number tab of the Format Cells dialog box. If they are not, then you need to format the cells using a Custom number format that displays the zeros. In both of these cases, the leading zeros will be included in the CSV file created by Excel.

This brings us to the second issue. When you load a CSV file into Excel, it tries to determine the format of the data being loaded. You probably noticed when you loaded your CSV file in Notepad that even though Excel includes leading zeros in the output file, there are no quotes around the field itself. This means that Excel automatically recognizes the field as a number when importing it. By default, then, the number is displayed using one of the number fields, thereby expunging any leading zeros in what Excel displays.

The way around this problem should be fairly obvious based on information earlier in this tip—somehow you need to get Excel to recognize the incoming information as text so that it treats the leading zeros as significant. The quickest way to do this is to follow these steps, prior to loading the CSV file:

  1. Make sure the CSV file is renamed so it has a TXT extension. You must perform this step, or the rest of the steps will not work because Excel won't start the Text Import Wizard in step 5.
  2. Display the Open dialog box. (In Excel 2007, click the Office button and then click Open. In Excel 2010, click the File tab of the ribbon and then click Open. In Excel 2013, click the File tab of the ribbon, click Open, then Computer, and finally Browse.)
  3. Using the Files of Type drop-down list at the bottom of the dialog box, indicate that you want to open Text Files (*.prn; *.txt; *.csv).
  4. Locate and select the file you renamed in step 1.
  5. Click on Open. Excel starts the Text Import Wizard, displaying the Step 1 of 3 dialog box. (See Figure 1.)
  6. Figure 1. The Text Import Wizard.

  7. Make sure the Delimited choice is selected, then click on Next. Excel displays the Step 2 of 3 dialog box.
  8. Make sure Comma is selected as a delimiter, then click on Next. Excel displays the Step 3 of 3 dialog box. The interesting thing is that the data in your TXT file should be displayed at the bottom of the dialog box, including any leading zeros in your fields.
  9. At the bottom of the dialog box, click on the field that has leading zeros. The entire column should now be selected.
  10. In the Column Data Format area, make sure the Text radio button is selected.
  11. Repeat steps 8 and 9 for any other fields that have leading zeros.
  12. Click on Finish. Your file is imported, with leading zeros still intact.

Now you can do your work in Excel, as desired, and again save your data in CSV format. (You will, however, need to use Save As rather than simply using Save.) The leading zeros will be included in the data that is saved.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10262) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Handling Leading Zeros in CSV Files.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Renaming a Macro

Want to give your macros a different name than they currently use? It's easy to do using the VBA Editor as described here.

Discover More

Changing the Color Used to Denote Selected Cells

When entering data into a range of cells, the cell in which you are working appears in a different color than the other cells ...

Discover More

Sudden Increases in Workbook File Size

Workbooks can get rather large rather quickly. If you think your workbook has gotten too big too fast, here are some things ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Avoiding Scientific Notation on File Imports

When importing information from a CSV file, you may get unintended results from time to time. Here's how to force Excel to ...

Discover More

Getting Rid of Extra Quote Marks in Exported Text Files

If you don't like the way that Excel exports information you intend to use with other programs, then your best bet is to ...

Discover More

Appending to a Non-Excel Text File

Does your macro need to add information to the end of a text file? This is called appending, and is done using the technique ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 - 0?

2017-02-24 19:21:14

eBiz

This is another case of MS NOT fixing their BROKEN STUFF/(J@#k) ... when any data is enclosed in quotes, in any language, that explicitly defines the data contained as STRING/TEXT ... in this case, IT SHOULD BE EXPECTED that EXCEL import this data as a STRING/TEXT type ... PERIOD! ... MS also provided a way to manually override CELL formats with a prefixed single quote as in '0001 = STRING(0001) ... Well even THIS is not accepted by a CSV/TXT import ... Macros can be built that follow special coded directions exported in the first row and column of any special export file ... HOWEVER ... EMBEDDED MACROS are NOW being rejected even by most security software and even MS's OS's sighting security risks. So while this tip is a known "MANUAL" method/KLUDGE to resolve a "ONE OF" import ... it is NOT acceptable for any useful day to day automation or high usage data processing.


2015-05-25 03:08:39

Clas Henrik

leading zeroes.

or like this,
the column is defined as numerical after csv import.
Format the column using Formatcells/custom as 0000000000 (number of characters = number of 0:s)
The column shows leading zeroes.
Open an empty notepad
Copy the column and paste into notepad
Back to the sheet, keep the column selected
start "text to columns" and import the selected column AS TEXT.
If you , i.e, had 00009999 prior "text to columns" was started you now get 9999 without leading zeroes.
Now you copy the notepad data and paste into your column in the sheet. The leading zeros remain.


2015-05-24 20:24:26

Alex B

If you need to do this a lot on your computer and don't want to use a macro, it may be worth breaking the association between a csv file and excel.

To break the association:
1) pick any csv file in Windows explorer.
2) right click on the file
3) select open with > choose default program
4) Select Notepad or WordPad, making sure the "Always use the selected program to open this kind of file", is selected.
5 ) hit OK

Your csv file icons will change to text icons.
This means you will need to open the file from within excel as you do for other text files and excel will no longer by pass the text import wizard.



2015-05-23 12:24:56

Steve Symes

I have this exact problem in my work, too. I generally resolve this by using a custom format for the column(s) concerned - I would have, say 0050 displayed as 50, so I use a custom format of 0000 to force the leading zeros to appear in the imported data.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.