如何在 Excel 2010 中使用 VBA 获取目录中最后修改的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6427081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the last modified file in a directory using VBA in Excel 2010
提问by Irfy
Im looking for a way to do this in Excel 2010 using VBA.
我正在寻找一种使用 VBA 在 Excel 2010 中执行此操作的方法。
It used to be possible in Excel 2003 using the Application.FileSearch method, but this has be depreciated. (see below)
曾经可以在 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
Any ideas how to do this in Excel 2010?
任何想法如何在 Excel 2010 中执行此操作?
Thanks
谢谢
回答by Richard Morgan
If using the FileSystemObject is acceptable, you could use the method described here.
如果可以接受使用 FileSystemObject,则可以使用此处描述的方法。
To summarize:
总结一下:
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