0

I am current using the code retrieved from Struggling to open most recent .xls in specified folder. File Not Found error. I would like to open several files when the active workbook is opened. Then, I would like to close all of the open files when the active workbook is closed. Can I loop the pathways for all of the documents to open? Please see the current code below:

Sub Auto_Open()
    Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder As Object
    Dim strFilename As String
    Dim dteFile As Date

    Const myDir As String = "G:\S\Staffing\Attendance\July 2016"

    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.path

        End If
    Next objFile

    Workbooks.Open strFilename

    Set FileSys = Nothing
    Set myFolder = Nothing
    Windows("dashboard.xlsm").Activate
End Sub
Community
  • 1
  • 1
  • Well... yeah. Just put that `Workbooks.Open strFilename` into your loop. – Tom K. Jul 29 '16 at 11:02
  • Update: I was able to add a second code for a second directory. I was also able to locate a code to close all of the Excel Windows. However, when I attempted to add a third code for a third directory I am receiving a run time error 1004. – Martrica Allumns Jul 31 '16 at 20:29
  • Thanks for the help! I was able to get it to work. – Martrica Allumns Aug 15 '16 at 16:06

0 Answers0