HKFuey
asked on
CSV to Excel date format
I use a SQL view to output data to a csv file, then this VBA (thanks to capricorn1) to convert the CSV to Excel format:
Sub saveasXLS()
Dim xlObj As Object, xlFile As String, csvFile
csvFile = "C:\xxx.csv"
xlFile = "C:\xxx.xls"
Set xlObj = CreateObject("excel.Applic ation")
xlObj.Workbooks.Open (csvFile)
xlObj.activeworkbook.savea s xlFile, FileFormat:=-4143, CreateBackup:=False
xlObj.activeworkbook.Saved = True
xlObj.Quit
End Sub
This works fine except for dates: -
File QtyReceived LastRecDate QtyOrdered LastOrderDate
CSV 44 10/01/2011 14:14 12 11/01/2011 12:13
XLS 44 01/10/2011 14:14 12 01/11/2011 12:13
The dates look OK in CSV but get messed up in XL, does anyone have a solution?
Sub saveasXLS()
Dim xlObj As Object, xlFile As String, csvFile
csvFile = "C:\xxx.csv"
xlFile = "C:\xxx.xls"
Set xlObj = CreateObject("excel.Applic
xlObj.Workbooks.Open (csvFile)
xlObj.activeworkbook.savea
xlObj.activeworkbook.Saved
xlObj.Quit
End Sub
This works fine except for dates: -
File QtyReceived LastRecDate QtyOrdered LastOrderDate
CSV 44 10/01/2011 14:14 12 11/01/2011 12:13
XLS 44 01/10/2011 14:14 12 01/11/2011 12:13
The dates look OK in CSV but get messed up in XL, does anyone have a solution?
ASKER
Hi Chris,
I'm not formatting the data it is straight out of SQL.
The date/time looks OK in SQL and when converted to CSV, it seems the xl conversion is corrupting it.
I'm not formatting the data it is straight out of SQL.
The date/time looks OK in SQL and when converted to CSV, it seems the xl conversion is corrupting it.
Ahhh, the standard problem...
dates and excel. The issue is probably localization. Excel is viewing your dates and deciding that you want to use US format - or is it UK format?
Trouble is you don't give an example of a date that doesn't work...or say which is right (dd/mm/yy or mm/dd/yy)
10/01 - can mean 10th January or 1st of October, you don't say which is which. Are you sure it's wrong? Have you got a date of 20th January? (20/01 or is it 01/20)...
Excel doesn't have to be wrong, it's just a different format...
get a whole month of sequential dates and then do the open...lets see if it is really wrong...
dates and excel. The issue is probably localization. Excel is viewing your dates and deciding that you want to use US format - or is it UK format?
Trouble is you don't give an example of a date that doesn't work...or say which is right (dd/mm/yy or mm/dd/yy)
10/01 - can mean 10th January or 1st of October, you don't say which is which. Are you sure it's wrong? Have you got a date of 20th January? (20/01 or is it 01/20)...
Excel doesn't have to be wrong, it's just a different format...
get a whole month of sequential dates and then do the open...lets see if it is really wrong...
you could also tell excel to open the file with text import wizard and then tell it the correct date format...that would help.
ASKER
Sorry for late reply, been on holiday!
I am in the UK and the format I want is DD/MM/YYYY
The access app I use collects data and emails it automatically (scheduled task) so I can't change things at run time it has to be the VBA that controls the output.
I originally just sent the CSV file and recipients had to save as XLS, I may have to go back to that method.
I am in the UK and the format I want is DD/MM/YYYY
The access app I use collects data and emails it automatically (scheduled task) so I can't change things at run time it has to be the VBA that controls the output.
I originally just sent the CSV file and recipients had to save as XLS, I may have to go back to that method.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have given some points for trying to help, thanks!
Chris