Automatically select/open most recent file

flightjock

New Member
Joined
Mar 18, 2002
Messages
48
We have to use data in a worksheet from an excel file that somebody else puts in a fixed directory on our network every day. Each day this source file has another name with the date included in the name. E.g. today's file would be called DRAAF20020426.xls and tomorrow's file would be called DRAAF20020427. The older files are not deleted.
Would it be possible to use VBA to automatically select the most recent file in this source directory and open it?

Any help or suggestion or hopefully solution would be very much appreciated.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi
Tested this.
Seems to work.
The only thing you should
need to do is add the correct path.<pre>

Sub FindLatestFile()
Dim i
Dim DateOne As Date
Dim DateTwo As Date
Dim YourPath As String
Dim YourFile As String

YourPath = "C:YourPath" 'change this to the correct path

With Application.FileSearch
.NewSearch
.LookIn = YourPath
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
If IsDate(Left(Right(.FoundFiles(i), 8), 2) & "/" & _
Left(Right(.FoundFiles(i), 6), 2) & "/" & _
Left(Right(.FoundFiles(i), 12), 4)) Then
DateTwo = Left(Right(.FoundFiles(i), 8), 2) & "/" & _
Left(Right(.FoundFiles(i), 6), 2) & "/" & _
Left(Right(.FoundFiles(i), 12), 4)
If DateTwo > DateOne Then
DateOne = DateTwo
YourFile = .FoundFiles(i)
End If
End If
Next i
Else
MsgBox "There were no files found."
End If
End With
Workbooks.Open YourFile
End Sub</pre>


Tom
This message was edited by TsTom on 2002-04-26 05:53
 
Upvote 0
Thanks,
However it does not work completely correct. It does open a file, but not the most recent one. I noticed that in the code you compare datetwo with dateone. However, I find no place in the code were dateone is calculated. Could this be the problem?

Tony
 
Upvote 0
Tom,
I found the problem. It has to to with date formatting. We use MM/DD/YYYY where you probably had MM/DD/YYYY. Now it works perfectly.

Thanks a lot for the fast solution
Tony
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,178
Members
449,071
Latest member
cdnMech

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