Open file with dynamic date and timestamp

giaudi

New Member
Joined
Jul 29, 2010
Messages
8
Hello, I need to write a code that can open a file which is saved to a common drive a number of times per day using today’s date with a time stamp.
The file format is saved as `filename-YYYY-MM-DD-HH-MM-SS.xls`. The problem is that the file is never saved at the same time and I wish to open the most recent one. Any idea?
Thanks. G
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Thanks James.

I am trying to adapt it to what I need. However this is just looking up for the last modified file in the folder, but let's say that in my folder I have tow types of files (aaaa-YYYY-MM-DD-HH-MM-SS.xls and bbbb-YYYY-MM-DD-HH-MM-SS.xls) and I just want to look up the last modified aaaa file? how should I change the beoow code?
Thanks

Sub GetMostRecentFile()

Dim FileSys As FileSystemObject
Dim objFile As File
Dim myFolder
Dim strFilename As String
Dim dteFile As Date

'set path for files - change for your folder
Const myDir As String = "\\xxxx\yyyy\zzzz"

'set up filesys objects
Set FileSys = New FileSystemObject
Set myFolder = FileSys.GetFolder(myDir)


'loop through each file and get date last modified. If largest date then store Filename
dteFile = DateSerial(1900, 1, 1)
For Each objFile In myFolder.Files
If objFile.DateLastModified > dteFile Then
dteFile = objFile.DateLastModified
strFilename = objFile.Name
End If
Next objFile
Workbooks.Open strFilename

Set FileSys = Nothing
Set myFolder = Nothing

End Sub
 
Upvote 0
Without testing it, maybe something like:

Code:
If objFile.DateLastModified > dteFile AND Left(objFile.Name,5) = "aaaaa" Then
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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