简体   繁体   English

在子文件夹中打开最近的文件夹并根据名称打开工作簿

[英]Open most recent folder within sub folder and open workbook based on name

As the title suggests, my objective is to open the most recently modified folder.正如标题所暗示的,我的目标是打开最近修改的文件夹。 Within this folder, I wish to open each sub folder and open each file with "summary" in it, then close.在这个文件夹中,我希望打开每个子文件夹并打开每个包含“摘要”的文件,然后关闭。 This is what i have so far:这是我到目前为止:

Option Explicit
Function GetLastFolder(Path As String)
    Dim FSO, FS, F, DtLast As Date, Result As String
    Set FSO = CreateObject("scripting.FileSystemObject")
    Set FS = FSO.GetFolder(Path).SubFolders
    For Each F In FS
        If F.DateLastModified > DtLast Then
             DtLast = F.DateLastModified
             Result = F.Name
        End If
    Next
    GetLastFolder = Result
End Function

Sub OpenFolder()

Dim wb As Workbook
Dim Folder As String

GetLastFolder ("H:\myFile\")

Dim numberOfFolders As Long

numberOfFolders = GetLastFolder.Files.Count

mylist = Array("File1", "File2", "File3")

For i = 0 To numberOfFolders
If Folder.Exists(mylist(i)) Then

End If

Next i

End Sub

My function locates the most recent folder and the sub procedure is supposed to do the rest.我的函数定位最新的文件夹,子过程应该完成其余的工作。 I'm hoping that my loop will enter each sub folder numberOfFolder amount of times which is called the name of the array before opening the file.我希望我的循环将在打开文件之前输入每个子文件夹numberOfFolder的次数,这被称为数组的名称。 My attempt is weak, especially when I arrive at the loop.我的尝试很弱,尤其是当我到达循环时。 I'm not sure how to reference folders and files which are not pre-defined.我不确定如何引用未预定义的文件夹和文件。

Your GetLastFolder seems fine.你的 GetLastFolder 看起来不错。

Try this OpenFolder to get you going:试试这个 OpenFolder 来助你一臂之力:

Sub OpenFolder()

    Dim wb As Workbook
    Dim Folder As String, sPath As String
    Dim numberOfFolders As Long
    Dim oFolder As Object, oSubFolder As Object, oFSO As Object

    sPath = "C:\Program Files\"
    numberOfFolders = 0
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Folder = GetLastFolder("C:\Program Files\")

    Set oFolder = oFSO.getfolder(sPath + Folder)

    For Each oSubFolder In oFolder.subfolders
        numberOfFolders = numberOfFolders + 1
    Next

    MsgBox numberOfFolders

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM