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

Handling Leading Zeros in CSV Files

by Allen Wyatt
(last updated June 11, 2014)

13

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. Within Excel, click on the Open tool on the toolbar. Excel displays the Open dialog box. (To display this dialog box in Excel 2007, click the Office button and then click on Open.)
  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. 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 (2588) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Putting Spreadsheet Names in Headers or Footers

One of the things you can add to your page header or footer is the name of your workbook file name. Here's how to make the ...

Discover More

Shortcut to Return to Document Text

When you are done typing a footnote or endnote in your document, you may want a way to return to the main document text ...

Discover More

Centering a Table

Left-justified tables are great for many document designs, but you may want instead to center a table between the margins of ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

Formatting Combo Box Text

If you insert objects, such as a combo box, in your worksheet, you may need a way to modify the font used in the object. The ...

Discover More

Adding Drop Shadows to Cells

Want to draw attention to what is in a cell? What better way than to add a drop shadow to that cell! Here's how you can do ...

Discover More

Displaying Latitude and Longitude

If you work with geographic data, you may need a way to display latitude and longitude in a worksheet. This tip examines ...

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 6Mpixels. 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 6 + 0?

2015-07-15 00:13:58

B

I followed everything, but when I saved it to csv format and open it again, the 0 disappears. Help! Thanks!


2015-04-29 01:28:52

Héjja Huba

Thanks for the tutorial! You saved my day!


2015-04-14 09:21:38

Greg

Pete Wilson ... Gold star! Exactly the solution I was after. No need to mess about in Excel. It just works.


2015-04-02 22:42:43

SubbaRao

The above tip helped a lot. Because In my case we are preparing data and sharing to client. so, we can add prefix and suffix as said by Wilson and solved the issue. Thanks.


2015-02-04 17:59:19

Charlie

This solution works to open a CSV file and have it formatted correctly but is there a way to maintain this after save? I have an AutoCAD program that pulls information from only a CSV file. however, once I save and then use autoCAD to import, the CSV sends that info as leading zero and instead of 00 in my drawing, I get 0. Or instead of 02-27-15 for a date, I get 02/27/2015. Can I just tell excel to stop changing my content?? Thanks


2014-11-12 02:41:38

justinhimt

Try this...

Excel.Range formatRange;
formatRange = xlWorkSheet.get_Range("a1", "b1");
formatRange.NumberFormat = "@";
xlWorkSheet.Cells[1, 1] = "098";

Source :

http://csharp.net-informations.com/excel/csharp-format-excel.htm

Justin


2014-10-21 19:09:13

Donna

Hi All,

Had this issue recently with trying to upload NDCs (National Drug Codes) with leading zeros. I opened the .csv file into OpenOffice and formatted the NDC column to be text as I opened the file. I formatted the NDC column again as follows once the file was open: ="00000000000 (NDCs are 11 digits long, but you can customize this to the length of your text string) I then saved the file as .txt
I hope this is helpful to someone with the same challenge.


2014-05-29 19:11:19

steve

this is great!


2013-02-27 20:34:50

Dave

Pete Wilson, you sir, are a champion.
Been looking for that solution everywhere.


2012-09-20 11:51:56

Peggy

Thank you! I couldn't remember how to do this, then searched & searched, & I so appreciate your so clearly describing the process.


2012-05-21 14:47:36

Pete Wilson

Apparently the only way is to convert the field to a string formula:

"=""string of data"""


2012-03-23 09:19:23

frank

This is not a good solution as it requires intervention before loading the data. Is there a way to tell excel to leave the data as is when directly importing a .csv file?


2011-11-17 17:39:39

Matheus

Great tip! Thank you.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.