L
Les Stout
Hi all, Tom Hutchins helped me out with some code and i need to change
it.
The code loops through a folder and looks at the last time the file was
modified to open the latest file and i need to look at the date stamp
when the file was created to get the newest file and not the last
modified.
Any help would be very much appreciated.
Sub AAAAA()
Const FilePath = "D:\Data\"
Workbooks.Open Filename:=FindNewestFile(FilePath)
End Sub
Function FindNewestFile(FilePath As String) As String
Dim LastDate As Date, NewDate As Date
Dim LastFile As String, NewFile As String
'Check all the .XLS files in the folder. Find the
'most recent file.
LastFile$ = LCase$(Dir(FilePath$ & "*.XLS"))
LastDate = FileDateTime(FilePath$ & LastFile$)
NewFile$ = LastFile$
Do While Len(NewFile$) > 0
NewFile$ = LCase$(Dir())
If Len(NewFile$) = 0 Then Exit Do
NewDate = FileDateTime(FilePath$ & NewFile$)
If NewDate > LastDate Then
LastDate = NewDate
LastFile$ = NewFile$
End If
Loop
FindNewestFile$ = FilePath$ & LastFile$
End Function
Best regards,
Les Stout
*** Sent via Developersdex http://www.developersdex.com ***
it.
The code loops through a folder and looks at the last time the file was
modified to open the latest file and i need to look at the date stamp
when the file was created to get the newest file and not the last
modified.
Any help would be very much appreciated.
Sub AAAAA()
Const FilePath = "D:\Data\"
Workbooks.Open Filename:=FindNewestFile(FilePath)
End Sub
Function FindNewestFile(FilePath As String) As String
Dim LastDate As Date, NewDate As Date
Dim LastFile As String, NewFile As String
'Check all the .XLS files in the folder. Find the
'most recent file.
LastFile$ = LCase$(Dir(FilePath$ & "*.XLS"))
LastDate = FileDateTime(FilePath$ & LastFile$)
NewFile$ = LastFile$
Do While Len(NewFile$) > 0
NewFile$ = LCase$(Dir())
If Len(NewFile$) = 0 Then Exit Do
NewDate = FileDateTime(FilePath$ & NewFile$)
If NewDate > LastDate Then
LastDate = NewDate
LastFile$ = NewFile$
End If
Loop
FindNewestFile$ = FilePath$ & LastFile$
End Function
Best regards,
Les Stout
*** Sent via Developersdex http://www.developersdex.com ***