Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date formats/values change when file is opened programmatically

Tags:

date

csv

excel

vba

I have .csv file which is a time series of daily data, with several data points associated with each date.

When I manually open the file, the dates open correctly, as the date format dd/mm/yyyy.

When I open the file programmatically, the dates up to the 12th of each month are opened as mm/dd/yyyy, although the format remains dd/mm/yyyy (e.g. the 1st of July 1983 (1/7/1983), would be opened as the 7th of January 1983 (7/1/1983) - this isn't just a formatting issue, the Julian Date (days since 1 Jan 1901) associated with these dates also changes), and the dates after the 12th of each month are opened correctly, although as text rather than a date.

The data coming in as text is not an issue, however, the dates changing as soon as the file is opened is problematic. I could try to import the entire .csv file as comma delimited text rather than opening the file, however, it would be easier and faster if I could stop the dates from changing when I open the file.

Flder = InputBox("Copy and Paste Folder path here:")

Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourcePath = FSO.GetFolder(Flder)

For Each File In SourcePath.Files        
    Workbooks.Open (File)

    FlNm = File.Name

    StrtCol = Workbooks(FlNm).Worksheets(1).Range(Cells(4, 1), Cells(4, 30)).Find ("Mean").Column

    Workbooks(FlNm).Worksheets(1).Range(Cells(1, 1), Cells(60000, 1)).Copy (Workbooks("Find Water Years V2.xls").Worksheets(1).Range("A3"))
    Workbooks(FlNm).Worksheets(1).Range(Cells(1, StrtCol), Cells(60000, StrtCol + 1)).Copy (Workbooks("Find Water Years V2.xls").Worksheets(1).Range("B3"))

    Workbooks(FlNm).Close
Next

The problem seems to occur at the line Workbooks.Open(File).

like image 423
Ben F Avatar asked Mar 08 '12 22:03

Ben F


2 Answers

Since the question has already been answered by the OP in the comments but not posted as an official answer I'll put it here in case someone else misses it like I did.

workbook = workbooks.Open(filename, Local:= true)

By setting Local = true uses the local machines date format rather than assuming mdy so in locales such as Australia (dmy) it'll change the behavior of Open()

like image 68
David Waterworth Avatar answered Sep 28 '22 15:09

David Waterworth


I experienced this problem with Office 365. With importing an csv file with Excel some dates dd/mm/jjjj (European) where imported as American dates mm/dd/jjjj. The csv file doesn't have any formatting of dates closed in.

By opening as follows:

Set xla = CreateObject("Excel.Application")
Set xlb = xla.Workbooks.Open(sFilePath, ReadOnly:=False, Local:=True)

The local settings where used, and problem solved :-)

Other options with opening a file are:

expression.Open (FileName, UpdateLinks, ReadOnly, 
                 Format, Password, WriteResPassword, 
                 IgnoreReadOnlyRecommended, Origin, 
                 Delimiter, Editable, Notify, Converter, 
                 AddToMru, Local, CorruptLoad)
like image 24
VDeen Avatar answered Sep 28 '22 15:09

VDeen