Solved

CSV to Excel date format

Posted on 2011-02-18
8
1,114 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:HKFuey
  • 4
  • 3
8 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34925125
Are you U using dd mm yyyy?

Chris
0
 

Author Comment

by:HKFuey
ID: 34925394
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.
0
 
LVL 5

Expert Comment

by:SMcP
ID: 34932377
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...
0
 
LVL 5

Expert Comment

by:SMcP
ID: 34932379
you could also tell excel to open the file with text import wizard and then tell it the correct date format...that would help.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:HKFuey
ID: 34949492
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.

0
 
LVL 5

Assisted Solution

by:SMcP
SMcP earned 250 total points
ID: 34958273
the best way to set this up right is to define your own OPEN Filter for the input file, rename it to txt

assuming you know EXCEL VBA (a least a bit) you should record a new macro and open the new txt file and in the final step tell excel the date format you are using for the specific columns.

The copy the workbooks.open statement into your original  VBA...

should work like a charm

regards
0
 

Accepted Solution

by:
HKFuey earned 0 total points
ID: 34978651
OK I have gone back to csv, the file size is smaller and it looks the same when opened.

0
 

Author Closing Comment

by:HKFuey
ID: 35015362
I have given some points for trying to help, thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now