Opening csv file with macro causes date format to change

NickGraham

New Member
Joined
Apr 14, 2009
Messages
2
Hi, I'm using some code I found on this forum to open a csv file. One of the columns contains dates and when opening the file using the macro the date format is changed from dd/mm/yyyy to mm/dd/yyyy but only if the date is on or before the 12th month. If the file is opened manually the date format stays as dd/mm/yyyy

The code I am using is:

Code:
Sub OpenCompletedWorkItem()
'opens the CompletedWorkItem file
Dim DateToOpen As String
DateToOpen = Format(Date, "yyyy-mm-dd")
'searches for all the CompletedWorkItem files with todays date
 With Application.FileSearch
        .NewSearch
        .LookIn = "J:\Dept\CSU TL\Batch Tracker\Ciboodle Data"
        .FileType = msoFileTypeExcelWorkbooks
        .Filename = "CompleteWorkItem_" & DateToOpen & "*"
        .Execute
        
        'counts the number of CompletedWorkItem files with todays date
        i = .FoundFiles.Count
        
        'opens the most recent CompletedWorkItem file
         Set wb = Workbooks.Open(.FoundFiles(i))
    End With
End Sub

It looks to me like Excel is defaulting to the US date format but I don't know how to instruct it that the date format is UK. Does anyone know how to fix this?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Don't you just hate the American date format? Causes no end of problems. The only way I know of to get csv data into Excel programmatically with the correct date format is to use a QueryTable to import. Otherwise you will have to process the dates after import to correct them.
 
Upvote 0
Hi,

This is a bit of a delayed reply, but I couldn't ignore the problem you've had, as I've had it myself in the past. There is a really easy way to set the date formats correctly.

If you use the following style of VB to open a CSV file, then the very last bit will set the dates correctly:

Workbooks.Open Filename:= _
"C:\My Folder\" & FileIWantToOpen & "", _
Local:=True

":=Local" is the key...

That should work!
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top