Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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

Written by Allen Wyatt (last updated May 9, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


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. In later versions of Excel, click the File tab of the ribbon and click 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, 2013, 2016, 2019, and Excel in Microsoft 365. 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

Putting Tables within Margins

When you first insert a table in your document, it extends from margin to margin. Later, after a bunch of editing and ...

Discover More

Sorting by Fill Color

Sorting data in a worksheet is easy, unless you want to sort by the color used to fill a range of cells. There are ways ...

Discover More

Typing Check Marks into Excel

Need to enter a check mark into a cell? There are a number of ways you can get the desired character, depending on the ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Who Has the File Open?

Open a workbook that someone else is working on, and you won't be able to save your changes back into the same file. ...

Discover More

Getting Rid of Empty Rows after Importing

Import data into a worksheet (or paste it there) and you may find that you end up with a group of blank cells you need to ...

Discover More

Saving in Multiple Locations

Need to save a workbook in more than one location? Here's a handy macro that can save your workbook in lots of different ...

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}] (all 7 characters, in the sequence shown) 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 2 + 2?

2020-06-20 20:19:00

Roy

Excel examines the first eight records of each field when importing and decides how to treat the column based upon that examination. That can be changed, but... amongst ofhter things, telling it to examine all the rows, say, could mean examining, say, a million records for each of, say, 30 fields... multiplies up, eh? And if none of your rows has characters like you need for the desired effect, it was pointless. One can open in Notepad, say, then insert patterned rows at the start (eight), then delete them after import though. But no one's USER is going to do any part of that, so those records would stay in.

Breaking the file association helps YOU, but only sometimes. I have files I want to import carefully, ones I simply want to open and overwrite, and some that the data in them isn't hurt by this, and the appearance, if it were somehow important (and it can be, though for me the importance is in the output spreadsheet which is already formatted for the material I move over), can be restored with a couple, not fifty, formatting changes. But no USER is ever going to be good with this. But mostly, break the association and you then have to hand-import everything.

Power Query (PQ) is a terrific thing, but it has its own problems, never touted by its proponents, nor even acknowledged. I can't set up a query, then apply it easily to a file with a different name from the one used. Rename the import files to that? Great... unless the currently named "that" is still in use by one or more Users. Hold off until some graet moment? Someone might use old data, or one of the "still in use by" Users might unknowingly use the new data which can lead to action/inaction issues. Think of a spreadhseet used for A/P calls, for example.

Worse, PQ has... its own "I know best" preferences. It like to essentially do a Paste|Special|Values with all formatting changed to Text. Likes may not be a strong enough word. And "essentially" is not fully accuraet: not just the all formatting changed to Text thing, but it also does Precision As Displayed (PAD) unless you override that. That is a HARD thing to notice with more than a wee bit af data being imported until solving some major problem gives you the go-ahead to really delve into it. Like I mentioned above, no one (in the "PQ is awesome, I even use it to cut my hair and boil pasta" camp) talks about it either, so it's a learn-by-screwing-up kind of thing. The fact you're trying to not work with the details —automating something — makes it easy to not worry about things too. It seriously does not want to import formulas, though that is not a problem with CSV material, of course, just a broader issue with PQ.

Easy to overcome all, they say, and it is, if you put the work into it AND have warning of all these things and more so you know the work to do, but... then you're right back to the problem that it is only for THAT data source, i.e., the filename you worked with and one has the issue from above. I'm not even aware of any way to set a preference for each of the import issues I've seen, or even some of them. Once again, like with simple CSV import, MS knows best, MS knows all, MS, like honeybadger, "don't care."

Sad as it is, writing your own import macros is really the only "industrial" (vs. "limping along from one battle to the nmext") solution. And if your organization dislikes macros, well, maybe some Indian company would like to open a new line of support service you could subscribe to.

Second alternative? Muddle through, put out the fires that arise. Each day. Take your lumps in meeting when you get blamed for everything. Accept that whipping boys don't get raises or promotions. And enjoy your vacation leaving the company-paid cellphone at home, whether they like that or not. (They expect you to take it along, and respond, but somehow, being paid for that day and saving the vacation day, does not logically follow...)


2020-05-09 10:03:42

Mark

Alex B's comment about doing this a lot of this is good.
I'd suggest another approach - Power Query. One of the great strengths of Power Query is that it works by recording your steps so you can have it repeat them on each new dataset. And if they need adjustment you can modify them as needed. And finally having a recording of them provides both an audit trail of the data manipulations and a way to share the process with others.
Note that in newer versions of Excel this is on the Data tab in Get & Transform Data to create them and in Queries & Connections to maintain them.


2020-05-09 09:39:25

Jeff Newman

two other ways around this are
1 duplicate the first data line and put alpha characters in the needed field. delete after import
2 many medical fields have leading zeros, but also characters in the same field
find one of these rows and make it the first data row. no deletion is necessary


2020-05-09 08:41:28

Alex B

To open the file in Notepad you don’t need to change the file extension. Just right click on the file and select “Open With” and then select Notepad.
If you then copy the text into excel from notepad you can access the conversion wizard using Data > Text to columns.
If you are doing a lot of this consider breaking the association between the csv file type and excel. If you change the association to say Notepad double clicking the file will open it in Notepad and in excel file open will launch the wizard.


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.