Excel - CSV leading zeros

Hi

I get sent an Excel workbook from a client that I need to save as a CSV file so that I can then use the CSV file with another piece of software. When I save the workbook as a CSV file, then when I open it again, the cells in column A don't retain the leading zero. In the workbook, the data in column A is entered as '012345.

Any ideas?

March 12th, 2015 7:36am

You can change values from 012345 to "012345". This will help Excel to recognize it as text and keep the zero.
Free Windows Admin Tool Kit Click here and download it now
March 12th, 2015 8:31am

As there can be sometimes several hundred cells where the leading zero has not been retained - I need a quick way to keep the zeros when the file is opened as a CSV.

I've tried selecting the entire column and creating a custom number format of 0000000 so there should always be 7 numbers. However, when I save and close and then reopen, I'm back to square one with no leading zeros. How can I get the CSV file to keep the zeros?

March 12th, 2015 9:03am

Re: keep leading zeros

Try running this on the selected cells; after custom formatting the cells with 0000000
'---
Sub NumericToText()
  Dim rCell As Range
  For Each rCell In Selection.Cells
    rCell.Value = "'" & rCell.Text
  Next
  Selection.NumberFormat = "@"
  For Each rCell In Selection.Cells
    rCell.Value = rCell.Text
  Next
End Sub
'---

Jim Cone
Portland, Oregon USA
free & commercial excel programs (n/a xl2013)
https://jumpshare.com/b/O5FC6LaBQ6U3UPXjOmX2

Free Windows Admin Tool Kit Click here and download it now
March 12th, 2015 10:15am

What do you mean by saying "leading zeros has not been retained"?

If you had an easy way to add quotes to the input data, would it help you?

A great tool to answer this need is Power Query.

March 12th, 2015 10:21am

When I save the Excel workbook as a CSV, and then open up the saved CSV file, the leading zeros are no longer there. These zeros need to be there so I can use this CSV file in another piece of software. Some cells have 6 numbers (no leading zeros) an other cells have 7 numbers (the ones that start with a number other than zero)
Free Windows Admin Tool Kit Click here and download it now
March 12th, 2015 10:31am

Can you import to CSV file to Excel using the Data->From Text command?

In Step 1 choose the delimited option.

In Step 2 select the delimiter.

In Step 3 choose the Column data format as Text.

This should import the numbers that starts with zero as you need.

March 12th, 2015 10:40am

Yes I've done that and the zeros appear, however when I close this file and open up the CSV file again the leading zeros are gone.
Free Windows Admin Tool Kit Click here and download it now
March 12th, 2015 11:13am

Please elaborate what you do when you close the file. Do you save it before you close? How do you open the file? Again as I wrote above through Data->From Text?
March 12th, 2015 11:17am

The problem is once the Excel file has been saved as a CSV one it then has to be imported into a totally different piece of software where the leading zeros NEED to be there. When I open the CSV file the leading zeros are not there.

I don't know how I can make this any clearer.

Free Windows Admin Tool Kit Click here and download it now
March 12th, 2015 11:27am

Please try this post.

March 12th, 2015 11:45am

I've seen this post already.
Free Windows Admin Tool Kit Click here and download it now
March 12th, 2015 12:11pm

There is no straight forward solution. We will consider to improve the experience and keep the leading zeros on save.

You can use XLSX workbook with automation (VBA, Power Query) that imports your client's CSV, save it as XLSX, then exports it as CSV with the trailing zeros. If you also need to work on the results from the other software (for example, to compare between input and output, or to further process it) you can import the CSV back to the XLSX.


March 12th, 2015 3:04pm

There is no straight forward solution. We will consider to improve the experience and keep the leading zeros on save.

You can use XLSX workbook with automation (VBA, Power Query) that imports your client's CSV, save it as XLSX, then exports it as CSV with the trailing zeros. If you also need to work on the results from the other software (for example, to compare between input and output, or to further process it) you can import the CSV back to the XLSX.


Free Windows Admin Tool Kit Click here and download it now
March 12th, 2015 7:02pm

There is no straight forward solution. We will consider to improve the experience and keep the leading zeros on save.

You can use XLSX workbook with automation (VBA, Power Query) that imports your client's CSV, save it as XLSX, then exports it as CSV with the trailing zeros. If you also need to work on the results from the other software (for example, to compare between input and output, or to further process it) you can import the CSV back to the XLSX.


March 12th, 2015 7:02pm

There is no straight forward solution. We will consider to improve the experience and keep the leading zeros on save.

You can use XLSX workbook with automation (VBA, Power Query) that imports your client's CSV, save it as XLSX, then exports it as CSV with the trailing zeros. If you also need to work on the results from the other software (for example, to compare between input and output, or to further process it) you can import the CSV back to the XLSX.


Free Windows Admin Tool Kit Click here and download it now
March 12th, 2015 7:02pm

There is no straight forward solution. We will consider to improve the experience and keep the leading zeros on save.

You can use XLSX workbook with automation (VBA, Power Query) that imports your client's CSV, save it as XLSX, then exports it as CSV with the trailing zeros. If you also need to work on the results from the other software (for example, to compare between input and output, or to further process it) you can import the CSV back to the XLSX.


March 12th, 2015 7:02pm

Hi,

Just checking in to see if the information was helpful. Please let us know if you would like further assistance.

Thanks

George Zhao
Forum Support
________________________________________
Come back and mark the replies as answers if they help and unmark them if they provide no help.
If you have any feedback on our support, please click "tnfsl@microsoft.com"

Free Windows Admin Tool Kit Click here and download it now
March 16th, 2015 9:26pm

The problem is once the Excel file has been saved as a CSV one it then has to be imported into a totally different piece of software where the leading zeros NEED to be there. When I open the CSV file the leading zeros are not there.

I don't know how I can make this any cl

March 16th, 2015 9:52pm

I'm seeing the client on Thursday - I'll report back after that. Thanks everybody for your help so far.
Free Windows Admin Tool Kit Click here and download it now
March 17th, 2015 11:38am

Hi VHaig,

Is there any update on this thread?

Regards,

George Zhao
Forum Support
________________________________________
Come back and mark the replies as answers if they help and unmark them if they provide no

help.
If you have any feedback on our support, please click "tnfsl@microsoft.com"

March 22nd, 2015 9:35pm

My client cancelled. You'll have to leave this one with me until I see him.
Free Windows Admin Tool Kit Click here and download it now
March 23rd, 2015 12:55pm

Hi,

Any update?

Regards,

Melon Chen

Forum Support

April 9th, 2015 8:47am

Hi,
I'm marking the reply as answer as there has been no update for a couple of days.
If you come back to find it doesn't work for you, please reply to us and unmark the answer.

Thanks

George Zhao
Forum Support
________________________________________
Come back and mark the replies as answers if they help and unmark them if they provide no help.
If you have any feedback on our support, please click "tnfsl@microsoft.com"

Free Windows Admin Tool Kit Click here and download it now
April 10th, 2015 2:38am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics