PDA

View Full Version : How to select & open latest time stamp workbook



YellowLabPro
10-07-2007, 04:21 AM
I download an exported workbook from my website daily. The name is the same, appended w/ a timestamp; example:
products_10-07-2007_06-54-am.xls

I need to select and open the most current one to copy updated data from it. How can I loop through the folder to find the latest one?

Path to folder is:
C:\Documents and Settings\Doug\Desktop\TGS\Exported Spreadsheets

Charlize
10-07-2007, 04:10 PM
Not tested but maybe something like this :Sub test_daily_files()
Dim mypath As String
Dim myfile As String
Dim vfound As Boolean
mypath = "C:\Documents and Settings\Doug\Desktop\TGS\Exported Spreadsheets\*.xls"
vfound = False
myfile = Dir(mypath)
Do While myfile <> ""
'products_10-07-2007_06-54-am.xls
If DateSerial(Split(Split(myfile, "_")(1), "-")(2), _
Split(Split(myfile, "_")(1), "-")(0), _
Split(Split(myfile, "_")(1), "-")(1)) = Date Then
vfound = True
Exit Do
Else
myfile = Dir
End If
Loop
If vfound = True Then
Workbooks.Open ("C:\Documents and Settings\Doug\Desktop\TGS\Exported Spreadsheets" & _
"\products_" & DateSerial(Split(Split(myfile, "_")(1), "-")(2), _
Split(Split(myfile, "_")(1), "-")(0), _
Split(Split(myfile, "_")(1), "-")(1)) & "_" & Split(myfile, "_")(2))
Else
MsgBox "No daily data was found !", vbInformation
End If
End Sub