Solved

CSV to Excel date format

Posted on 2011-02-18
8
1,108 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

21 Experts available now in Live!

Get 1:1 Help Now