Open Latest created file

Domtflowers

New Member
Joined
Mar 18, 2004
Messages
14
i know of an open latest created file syntax but down't know what it is can anyone help also how do i specify the file path (where to look for the latest file)
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
This is how I would do the job. Be interestd to see your "latest created" syntax.

Code:
Sub FIND_NEWEST_FILE()
    Dim MyFolder As String
    Dim MyFileName As String
    Dim MyDateTime As Date
    Dim NewestFile As String
    Dim NewestDateTime As Date
    MyFolder = CurDir & "\"
    '---------------------------
    MyFileName = Dir(MyPath & "*.XLS")
    Do While MyFileName <> ""
        MyDateTime = FileDateTime(MyFolder & MyFileName)
        If MyDateTime > NewestDateTime Then
            NewestDateTime = MyDateTime
            NewestFile = MyFileName
        End If
        MyFileName = Dir
    Loop
    'MsgBox (NewestFile & vbCr & NewestDateTime)
    Workbooks.Open FileName:=MyFolder & NewestFile
End Sub
 
Upvote 0
Brian, should this line:

MyFileName = Dir(MyPath & "*.XLS")

be:

MyFileName = Dir(MyFolder & "*.XLS")

Apologies in advance, if i am wrong
 
Upvote 0
Yes. Well spotted. sorry for that. I did test it before posting and it worked correctly despite that.

Although the code does work on my machine, I think the line
MyFolder = CurDir
(which refers to the folder which is currently active.)
should be used instead of MyFolder = CurDir & "\"

Hope you find it useful.
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,013
Members
448,935
Latest member
ijat

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