Leading zero's missing after XLSX to CSV macro

Lockie

New Member
Joined
Sep 6, 2022
Messages
5
Platform
  1. Windows
Hi everyone,

First time poster here (please be gentle!). Terribly sorry if this has already been seen before and fixed, again I'm new here, just hoping to learn a little code... Not a wizz with macros and just trying to fix a batch XLSX to CSV macro issue I've got.

Anyway, the code works fine generally, but unfortunately when the CSVs are output I lose all of the leading 0's to every cell, which I need for my Python code to later read time/dates properly. I can only tell this when I open the CSV files in notepad++ and they simply aren't there, otherwise the CSVs look identical.

Here's my code below.

Any help would be greaaaatly appreciated.


VBA Code:
Sub WorkbooksSaveAsCsvToFolder()

'Update

Dim xObjWB As Workbook

Dim xObjWS As Worksheet

Dim xStrEFPath As String

Dim xStrEFFile As String

Dim xObjFD As FileDialog

Dim xObjSFD As FileDialog

Dim xStrSPath As String

Dim xStrCSVFName As String

Dim xS  As String

    Application.ScreenUpdating = False

    Application.EnableEvents = False

    Application.Calculation = xlCalculationManual

    Application.DisplayAlerts = False

    On Error Resume Next

Set xObjFD = Application.FileDialog(msoFileDialogFolderPicker)

    xObjFD.AllowMultiSelect = False

    xObjFD.Title = "Select a folder which contains XLSX files"

    If xObjFD.Show <> -1 Then Exit Sub

    xStrEFPath = xObjFD.SelectedItems(1) & "\"

    Set xObjSFD = Application.FileDialog(msoFileDialogFolderPicker)

 
    xObjSFD.AllowMultiSelect = False

    xObjSFD.Title = "Select a folder to send CSV files"

    If xObjSFD.Show <> -1 Then Exit Sub

    xStrSPath = xObjSFD.SelectedItems(1) & "\"


    xStrEFFile = Dir(xStrEFPath & "*.xls*")


    Do While xStrEFFile <> ""

       xS = xStrEFPath & xStrEFFile

        Set xObjWB = Application.Workbooks.Open(xS)

        xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, ".") + 50)
       
        xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, "xl") - 2) & ".csv"

        xObjWB.SaveAs Filename:=xStrCSVFName, FileFormat:=xlCSV

        xObjWB.Close savechanges:=False

        xStrEFFile = Dir

  Loop

    Application.Calculation = xlCalculationAutomatic

    Application.EnableEvents = True

    Application.ScreenUpdating = True

    Application.DisplayAlerts = True

End Sub
 
Last edited by a moderator:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
The format of the CSV file will be as displayed in the workbook. Do the cells in the workbooks display with leading 0s when opened?
 
Upvote 0
Are you sure that they are showing in the Excel file to start with?
And are you absolutely sure that you are not opening the CSV file in Excel after exprorting it?

Typically, if you have an Excel file what shows leading zeroes, and the file is saved as a CSV, it will keep the loading zeroes.
However, if you then open that CSV file in Excel, it will drop them at that point (as Excel performs its own data conversions based on what it thinks that data type is).

If you view the virgin CSV file in a Text Editor (right after it is exported to CSV), you should still see the leading zeroes.
 
Upvote 0
The format of the CSV file will be as displayed in the workbook. Do the cells in the workbooks display with leading 0s when opened?
Hi there,

Yes - in Excel the leading 0's are there in the workbook, but when I actually open the macro made CSV in notepad++ they're gone. This doesn't happen if I manually open and save the workbooks and CSVs (but obviously I'm looking for a macro as I have 100's of these to turn into CSVs).

I've attached a screenshot of how it should look, I.e. 01 Aug rather than 1 Aug, etc etc.

Thanks in advance for your help!
 

Attachments

  • macro issue.jpg
    macro issue.jpg
    163.7 KB · Views: 23
Upvote 0
And just an example of how the macro version comes out as...
 

Attachments

  • macro issue2.jpg
    macro issue2.jpg
    139.1 KB · Views: 19
Upvote 0
Do you see the leading zeroes in the original Excel file, before exporting?
Can you post a sample of the data from the Excel file, so we can run your code against the actual data you have?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Do you see the leading zeroes in the original Excel file, before exporting?
Can you post a sample of the data from the Excel file, so we can run your code against the actual data you have?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

Sure thing - sorry took a few moments to get that add-in sorted.

See my minisheet:

Formby_2.9_July_2022.xlsx
BCDEHIJKNOPQ
1
2
3Tidal Station:FormbyYear:2022
4
5Sun Angle:Height above Chart Datum: H = SD-CD:2.9
6
7JulyAugustSeptember
8
10DateStart TimeEnd TimeDurationDateStart TimeEnd TimeDurationDateStart TimeEnd TimeDuration
1101-Aug05:2509:0503:4001-Sep06:0509:4703:4200:00
1201-Aug17:3821:0203:2401-Sep18:2421:5003:2600:00
1302-Aug06:0109:3903:3802-Sep06:4810:1703:2900:00
1402-Aug18:1721:3503:1802-Sep19:1522:2103:0600:00
1503-Aug06:4010:1203:3203-Sep07:4710:4602:5900:00
1603-Aug19:0122:0803:0703-Sep20:2622:5402:2800:00
1704-Aug07:2310:4503:2204-Sep09:1411:1702:0300:00
1804-Aug19:5222:4202:5004-Sep22:0723:3501:2800:00
1905-Aug08:1611:2203:0605-Sep23:4601:0601:2000:00
2005-Aug20:5723:2202:2506-Sep12:3913:5701:1800:00
2106-Aug09:2712:0802:4107-Sep00:3903:1402:3500:00
2206-Aug22:1800:1401:5607-Sep13:1715:5102:3400:00
2307-Aug10:5413:1302:1908-Sep01:2204:5103:2900:00
2407-Aug23:3801:3701:5908-Sep13:5517:1103:1600:00
2508-Aug12:1214:3902:2709-Sep02:0405:5903:5500:00
2609-Aug00:3803:1902:4109-Sep14:3218:1103:3900:00
2709-Aug13:0916:0602:5710-Sep02:4406:5304:0900:00
2810-Aug01:2704:5003:2310-Sep15:1019:0103:5100:00
2910-Aug13:5717:2103:2411-Sep03:2507:3804:1300:00
3011-Aug02:1206:0403:5211-Sep15:4819:4403:5600:00
3111-Aug14:4218:2303:4112-Sep04:0608:1704:1100:00
3212-Aug02:5707:0504:0812-Sep16:2720:2303:5600:00
3312-Aug15:2519:1603:5113-Sep04:4808:5204:0400:00
3413-Aug03:4207:5704:1513-Sep17:0820:5803:5000:00
3513-Aug16:0920:0403:5514-Sep05:2909:2303:5400:00
3614-Aug04:2908:4304:1414-Sep17:5121:2803:3700:00
3714-Aug16:5420:4703:5315-Sep06:1209:4703:3500:00
3815-Aug05:1509:2404:0915-Sep18:3821:5303:1500:00
3915-Aug17:3921:2603:4716-Sep07:0210:0503:0300:00
4016-Aug06:0110:0003:5916-Sep19:3922:0802:2900:00
4116-Aug18:2622:0103:3517-Sep08:1210:0601:5400:00
4217-Aug06:4810:3203:4422-Sep01:5203:5101:5900:00
4317-Aug19:1722:3103:1422-Sep14:1815:5001:3200:00
4418-Aug07:4110:5803:1723-Sep02:0505:0402:5900:00
4518-Aug20:2022:5502:3523-Sep14:2217:0502:4300:00
4619-Aug08:5011:1602:2624-Sep02:2705:5403:2700:00
4719-Aug22:0022:5800:5824-Sep14:4017:5403:1400:00
4824-Aug02:1204:2402:1225-Sep02:5206:3403:4200:00
4924-Aug14:2716:3102:0425-Sep15:0418:3603:3200:00
5025-Aug02:3305:3202:5926-Sep03:2007:1003:5000:00
5125-Aug14:4417:3302:4926-Sep15:3119:1403:4300:00
5226-Aug02:5706:2103:2427-Sep03:4907:4503:5600:00
5326-Aug15:0718:2003:1327-Sep16:0119:5003:4900:00
5427-Aug03:2307:0103:3828-Sep04:2108:1903:5800:00
5527-Aug15:3219:0003:2828-Sep16:3520:2503:5000:00
5628-Aug03:5107:3703:4629-Sep04:5708:5103:5400:00
5728-Aug16:0019:3703:3729-Sep17:1420:5903:4500:00
5829-Aug04:2108:1203:5130-Sep05:3809:2203:4400:00
5929-Aug16:3120:1203:4130-Sep17:5921:3203:3300:00
6030-Aug04:5308:4503:5200:0000:00
6130-Aug17:0520:4603:4100:0000:00
6231-Aug05:2809:1703:4900:0000:00
6331-Aug17:4221:1803:3600:0000:00
Formby_2.9
Cell Formulas
RangeFormula
B7B7=VLOOKUP(CZ1,DA1:DD13,2,FALSE)
H7H7=VLOOKUP(CZ1,DA1:DD13,3,FALSE)
N7N7=VLOOKUP(CZ1,DA1:DD13,4,FALSE)
E11:E63,Q11:Q63,K11:K63E11=SUM(D11-C11)
 
Upvote 0
I tried your code on your sample data above, and it kept the leading zeroes, i.e.

1662474127239.png


Note that how it also copied the headers and all the other stuff.
If your file does not look like that, then it seems to suggest that maybe there is a step in there than you are missing, that is doing some other data conversions or formatting first.
 
Upvote 0
Try changing the saveas line to this:

VBA Code:
xObjWB.SaveAs Filename:=xStrCSVFName, FileFormat:=xlCSV, local:=true
 
Upvote 0
Solution
Rory, you are a true legend. That works!!!

Thank you both for your help - your generosity in helping is really really appreciated.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top