curmudge0n
New Member
- Joined
- Sep 11, 2011
- Messages
- 3
Hello,
What this code does: Opens the latest modified file in my directory.
Problem: After my first run of the macro, I keep having to save the file via "SaveAs" for example if i'm moving my test file to my current folder (it is not because I am saving my macro sheet, thus making my file the most recent). After I run the macro once, I have to save via SaveAs to avoid the 1004 error.
The code is erroring out on line "Workbooks.Open strFilename"
Again, this code works fine after the first run- is there anything I can do to avoid this 1004 error for subsequent runs?
What this code does: Opens the latest modified file in my directory.
Problem: After my first run of the macro, I keep having to save the file via "SaveAs" for example if i'm moving my test file to my current folder (it is not because I am saving my macro sheet, thus making my file the most recent). After I run the macro once, I have to save via SaveAs to avoid the 1004 error.
The code is erroring out on line "Workbooks.Open strFilename"
Again, this code works fine after the first run- is there anything I can do to avoid this 1004 error for subsequent runs?
Code:
Option Explicit
Sub zzz()
Dim FileSys As FileSystemObject
Dim objFile As File
Dim myFolder
Dim strFilename As String
Dim dteFile As Date
Dim myDir As String
myDir = ThisWorkbook.Path
Set FileSys = New FileSystemObject
Set myFolder = FileSys.GetFolder(myDir)
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
Last edited: