[Resolved] Open Last Modified File getting Error 1004

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?


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:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Re: Open Last Modified File getting Error 1004

Will change to "resolved" - just had to remove Option Explicit as it was part of a bigger macro that was not using variables.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,348
Members
452,907
Latest member
Roland Deschain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top