Solved

CSV to Excel date format

Posted on 2011-02-18
8
1,127 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

626 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