使用VBA在不同文件夹中打开多个最新文件,然后在关闭活动工作簿时关闭所有文件

时间:2016-07-28 20:01:12

标签: excel vba excel-vba directory

我目前正在使用从中检索到的代码 Struggling to open most recent .xls in specified folder. File Not Found error。我想打开活动工作簿时打开几个文件。然后,我想关闭活动工作簿时关闭所有打开的文件。我可以循环打开所有文档的路径吗?请参阅下面的当前代码:

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

0 个答案:

没有答案