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)
 

Some videos you may like

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

BrianB

Well-known Member
Joined
Feb 17, 2003
Messages
8,127
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
 

P Sitaram

Well-known Member
Joined
Jun 24, 2003
Messages
1,974
Brian, should this line:

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

be:

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

Apologies in advance, if i am wrong
 

BrianB

Well-known Member
Joined
Feb 17, 2003
Messages
8,127
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,085
Messages
5,599,650
Members
414,325
Latest member
kfg1287

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
Top