Open Last Modified Excel File From A Folder Through Vba
Dear All,
We can use below vba code to open last modified excel file from a folder.
Sub Last_Modified_File()
x = InputBox(“put Folder Path”)
folder_name = x & “\”
flname = Dir(folder_name & “*.xlsx”)
Do While Len(flname) > 0
dttime = FileDateTime(folder_name & flname)
If dttime > qq Then
qq = dttime
End If
flname = Dir()
Loop
folder_name = x & “\”
flname = Dir(folder_name & “*.xlsx”)
Do While Len(flname) > 0
dttime = FileDateTime(folder_name & flname)
If qq = dttime Then
‘qst = folder_name & flname
Workbooks.Open folder_name & flname
Exit Sub
End If
flname = Dir()
Loop
End Sub
7:48 am
Sorry, but that’s terrible VBA! You haven’t declared a single variable and you don’t handle the possibility that InputBox returns an invalid value. You obviously have not set VBA up to require variable declaration (a terrible mistake: I’ve never understood why it’s not the default!). I’ll overlook that you don’t assign an initial value to qq (since VBA will initialize it to 0), but even that’s sloppy programming!