Hi excel Peeps! I am trying to use VBA to automatically open an existing excel file in a folder in my documents with the lastest date modified. I seem to be running into a problem with the code below. When the file path is
sPath = ThisWorkbook.Path
it finds the newest file and opens it but I need to specify the file path and when I set the path to
sPath = "C:\Users\LEU70963\Documents\TRACKER JUNK\AT"
I get a "Run-time error '1004': "could not be found. Check the spelling of the file name, and verify that the file location is correct."
Any help would be huge!
Sub OpenLastModifiedFile()
Dim objFSO As Object, objFolder As Object, objFile As Object
Dim tmpDate As Date, sFile As String, sPath As String
'sPath = ThisWorkbook.Path
sPath = "C:\Users\LEU70963\Documents\TRACKER JUNK\AT"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(sPath)
For Each objFile In objFolder.Files
If objFSO.GetExtensionName(objFile.Path) = "xls" Then
If objFile.DateLastModified > tmpDate Then
tmpDate = objFile.DateLastModified
sFile = objFile.Path
End If
End If
Next
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Workbooks.Open sFilef
End Sub
Thank you
Alex
Bookmarks