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.
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: