Date format in VB created CSV file

gaz_chops

Well-known Member
Joined
Apr 29, 2003
Messages
6,485
Platform
  1. MacOS
I have a small bit of code that copies a sheet from Excel and creates a CSV file and saves it, this works fine, however when I try and import the file into Xero (finance software), the date format in the CSV file has changed from UK to USA format!

In Excel it shows as
25/06/2021​

In newly created CSV file it shows as

6/25/2021

Anyway to retain the original date format?

Thanks
Gaz
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
If you open the csv in a text editor, such as notepad, what is the date?
 
Upvote 0
If you open the csv in a text editor, such as notepad, what is the date?
This is in notepad

VBA Code:
*Narration,*Date,Description,*AccountCode,*TaxRate,*Amount,TrackingName1,TrackingOption1,TrackingName2,TrackingOption2
Monthly Payroll Summary,6/25/2021,Gross Pay,6020,No Tax," 4,750.87 ",,,,
Monthly Payroll Summary,6/25/2021,Gross Pay,6029,No Tax," 7,195.72 ",,,,
Monthly Payroll Summary,6/25/2021,Gross Pay,6041,No Tax," 3,901.40 ",,,,
 
Upvote 0
Not sure what's going on. If I manually save a sheet as a csv the dates remain as UK dates, but doing the same thing from a macro converts the dates to US style.
I've used VBA to create csv files countless times & never had this problem.
 
Upvote 0
HI, have you tried setting the "local" argument of the SaveAs method to TRUE (assuming you are using the SaveAs method to create the csv file!)?
 
Upvote 0
HI, have you tried setting the "local" argument of the SaveAs method to TRUE (assuming you are using the SaveAs method to create the csv file!)?
This is the code i am using, where would i add/amend it to include the "local" argument?

VBA Code:
VBA Code:
Sub XeroCSV()
'
' XeroCSV Macro
'

'
    Dim FileName As String
    Dim Path As String
    Path = "H:\Documents\Xero Reports\IRIS Payroll Data for Xero\"
    FileName = Range("G2").Value & ".csv"
    Sheets("Xero Import").Select
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Path & FileName, xlCSV
    Application.DisplayAlerts = True
    ActiveWindow.Close
    Sheets("Journal").Select
    Range("G5").Select
End Sub
 
Upvote 0
Try
VBA Code:
Sub XeroCSV()
'
' XeroCSV Macro
'

'
    Dim FileName As String
    Dim Path As String
    Path = "H:\Documents\Xero Reports\IRIS Payroll Data for Xero\"
    FileName = Range("G2").Value & ".csv"
    Sheets("Xero Import").Select
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs FileName:=Path & FileName, FileFormat:=xlCSV, Local:=True
    Application.DisplayAlerts = True
    ActiveWindow.Close
    Sheets("Journal").Select
    Range("G5").Select
End Sub
But you should avoid using VBA keywords (such as Path & Filename) as the names of variables.
 
Upvote 0
Try
VBA Code:
Sub XeroCSV()
'
' XeroCSV Macro
'

'
    Dim FileName As String
    Dim Path As String
    Path = "H:\Documents\Xero Reports\IRIS Payroll Data for Xero\"
    FileName = Range("G2").Value & ".csv"
    Sheets("Xero Import").Select
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs FileName:=Path & FileName, FileFormat:=xlCSV, Local:=True
    Application.DisplayAlerts = True
    ActiveWindow.Close
    Sheets("Journal").Select
    Range("G5").Select
End Sub
But you should avoid using VBA keywords (such as Path & Filename) as the names of variables.
Thanks, i'll try it when in work tomorrow.

Gaz
 
Upvote 0
Try
VBA Code:
Sub XeroCSV()
'
' XeroCSV Macro
'

'
    Dim FileName As String
    Dim Path As String
    Path = "H:\Documents\Xero Reports\IRIS Payroll Data for Xero\"
    FileName = Range("G2").Value & ".csv"
    Sheets("Xero Import").Select
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs FileName:=Path & FileName, FileFormat:=xlCSV, Local:=True
    Application.DisplayAlerts = True
    ActiveWindow.Close
    Sheets("Journal").Select
    Range("G5").Select
End Sub
But you should avoid using VBA keywords (such as Path & Filename) as the names of variables.
That doesn't appear to work, dates are still wrong in the CSV file!

VBA Code:
*Narration    *Date    Description    *AccountCode
Monthly Payroll Summary    6/25/2021    Gross Pay    6020
Monthly Payroll Summary    6/25/2021    Gross Pay    6029
 
Upvote 0
OK it looks like i've fixed it, just for anyone else who may read this, I did the following -

I entered the formula =text(cell ref,"dd/mm/yyyy") in the cell that was picking up the date, it now shows in the correct format in the CSV file that is created.

VBA Code:
*Narration    *Date    Description
Monthly Payroll Summary    25/06/2021    Gross Pay
Monthly Payroll Summary    25/06/2021    Gross Pay
Monthly Payroll Summary    25/06/2021    Gross Pay
 
Upvote 0
Solution

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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