Solved

CSV to Excel date format

Posted on 2011-02-18
8
1,121 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

713 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