Link to home
Start Free TrialLog in
Avatar of HKFuey
HKFueyFlag for United Kingdom of Great Britain and Northern Ireland

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.Application")
    xlObj.Workbooks.Open (csvFile)
    xlObj.activeworkbook.saveas 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?
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you U using dd mm yyyy?

Chris
Avatar of HKFuey

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.
Avatar of SMcP
SMcP

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...
you could also tell excel to open the file with text import wizard and then tell it the correct date format...that would help.
Avatar of HKFuey

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.

SOLUTION
Avatar of SMcP
SMcP

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HKFuey

ASKER

I have given some points for trying to help, thanks!