【问题标题】:How to get the last modified file in a directory using VBA in Excel 2010如何在 Excel 2010 中使用 VBA 获取目录中最后修改的文件
【发布时间】:2011-09-19 14:28:41
【问题描述】:

我正在寻找一种使用 VBA 在 Excel 2010 中执行此操作的方法。

过去在 Excel 2003 中使用 Application.FileSearch 方法是可能的,但它已被贬低。 (见下文)

Dim sFileName As String

sFileName = ""
With Application.FileSearch
    .NewSearch
    .LookIn = sDir
    .Filename = "*.*"
    .Execute msoSortByLastModified, msoSortOrderDescending

    If .FoundFiles.Count > 0 Then sFileName = .FoundFiles(1)

End With

任何想法如何在 Excel 2010 中执行此操作?

谢谢

【问题讨论】:

    标签: excel vba excel-2010 file-search


    【解决方案1】:

    如果使用 FileSystemObject 是可以接受的,您可以使用here 描述的方法。

    总结一下:

    Dim fso As Scripting.FileSystemObject
    Dim fol As Scripting.Folder
    Dim fdr As Scripting.Folder
    Dim fil As Scripting.File
    Dim flc As Scripting.Folders
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fol = fso.GetFolder("YourPathName")
    Set flc = fol.SubFolders
    
    For Each fdr In flc
      For Each fil In fdr.Files
            Debug.Print fil.DateLastModified
      Next fil
    Next fdr
    
    Set fso = Nothing
    Set fol = Nothing
    Set flc = Nothing
    

    【讨论】:

    • 谢谢理查德,工作愉快。只需要遍历文件列表,维护一个包含 DateLastModified 最大的文件的变量。
    • 顺便说一句,这样做也意味着它适用于所有 Excel 2003/2007/2010(至少)。
    • 不工作,他的发现不是最后修改但最后循环
    猜你喜欢
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多