VBA - 如何在 Excel 2010 中获取目录中最后修改的文件或文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6656023/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:37:29  来源:igfitidea点击:

VBA - How to get the last modified file or folder in a directory in Excel 2010

excelvbafile-iolast-modified

提问by Brian

What I want to do is more complex than selecting a file from a list of files. I will start in a directory, then I want to change to the most recently modified directory. I then want to repeat that process in a sub-directory, and then, inside of that, I want to select the most recently modified excel file and open it.

我想要做的比从文件列表中选择一个文件更复杂。我将从一个目录开始,然后我想更改到最近修改的目录。然后我想在一个子目录中重复这个过程,然后在里面,我想选择最近修改的 excel 文件并打开它。

What is the best approach to do this?

做到这一点的最佳方法是什么?

What objects / methods should I be looking into?

我应该研究哪些对象/方法?

回答by Lance Roberts

The simplest function is

最简单的函数是

FileDateTime(pathname)

where pathname can be a directory for folder.

其中 pathname 可以是文件夹的目录。

Alternatively, you can use the FileSystemObject object, DateLastModified property:

或者,您可以使用 FileSystemObject 对象的 DateLastModified 属性:

Dim fileModDate As String

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(<filenamestringhere>)

fileModDate = f.DateLastModified

All of the above can be explored in VBA help.

以上所有内容都可以在 VBA 帮助中进行探索。