VBA在文件夹中生成最近修改的.XLS文件

时间:2018-06-07 18:45:13

标签: excel vba excel-vba last-modified

我可以使用哪些VBA代码来使用单元格中显示的文件夹路径来检索该文件夹中最近修改过的.xls文件?到目前为止,我有文件名显示但不是正确的文件:

Function GetFileNames(ByVal FolderPath As String) As Variant
Dim Result As Variant
Dim i As Integer
Dim MyFile As Object
Dim MyFSO As Object
Dim MyFolder As Object
Dim MyFiles As Object
Set MyFSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = MyFSO.GetFolder(FolderPath)
Set MyFiles = MyFolder.Files
ReDim Result(1 To MyFiles.Count)
i = 1
For Each MyFile In MyFiles
Result(i) = MyFile.Name
i = i + 1
Next MyFile
GetFileNames = Result
End Function

2 个答案:

答案 0 :(得分:0)

您只需要检查文件夹中每个文件的DateLastModified时间戳。快速检查以确定它是否是最新的"排序"它到了顶部。

Option Explicit

Sub test()
    Debug.Print "most recently modified file is " & GetNewestModifiedFilename("C:\Temp")
End Sub

Function GetNewestModifiedFilename(ByVal folderPath As String, _
                                   Optional fileType As String = "xls*") As String
    Dim MyFSO As Object
    Dim MyFolder As Object
    Dim MyFiles As Object
    Set MyFSO = CreateObject("Scripting.FileSystemObject")
    Set MyFolder = MyFSO.GetFolder(folderPath)
    Set MyFiles = MyFolder.Files

    Dim mostRecentFilename As String
    Dim mostRecentTimestamp As Date
    Dim MyFile As Object
    For Each MyFile In MyFiles
        Debug.Print MyFile.Name & ", modified " & MyFile.DateLastModified
        If Mid(MyFile.Name, InStrRev(MyFile.Name, ".") + 1) Like fileType Then
            If MyFile.DateLastModified > mostRecentTimestamp Then
                mostRecentFilename = MyFile.Name
                mostRecentTimestamp = MyFile.DateLastModified
            End If
        End If
    Next MyFile
    GetNewestModifiedFilename = mostRecentFilename
End Function

答案 1 :(得分:0)

我认为您所寻找的内容类似于this question的选定答案。

您可以调整代码以满足您在下面的函数中传递参数的特定需求。请注意,参数目录必须在末尾包含反斜杠(例如“C:\ Users \”)。

Function NewestFile(Directory As String) As String
'PURPOSE: Get the newest file name from specified directory
Dim FileName As String
Dim MostRecentFile As String
Dim MostRecentDate As Date
Dim FileSpec As String

'Specify the file type, if any
 FileSpec = "*.xls"
FileName = Dir(Directory & FileSpec)

If FileName <> "" Then
    MostRecentFile = FileName
    MostRecentDate = FileDateTime(Directory & FileName)
    Do While FileName <> ""
        If FileDateTime(Directory & FileName) > MostRecentDate Then
             MostRecentFile = FileName
             MostRecentDate = FileDateTime(Directory & FileName)
        End If
        FileName = Dir
    Loop
End If

NewestFile = MostRecentFile

End Function

编辑:为了获得更大的灵活性,您还可以添加选项(如在PeterT的修订答案中),以使用可选的 FileSpec 参数搜索其他类型的文件,例如下面的替代功能。对于此功能,如果您没有为 FileSpec 提供任何值,它将查看所​​有文件。

Function NewestFile(ByVal Directory As String, Optional ByVal FileSpec As String = "*.*") As String
'PURPOSE: Get the newest .xls file name from
Dim FileName As String
Dim MostRecentFile As String
Dim MostRecentDate As Date

'Specify the file type, if any
FileName = Dir(Directory & FileSpec)

If FileName <> "" Then
    MostRecentFile = FileName
    MostRecentDate = FileDateTime(Directory & FileName)
    Do While FileName <> ""
        If FileDateTime(Directory & FileName) > MostRecentDate Then
             MostRecentFile = FileName
             MostRecentDate = FileDateTime(Directory & FileName)
        End If
        FileName = Dir
    Loop
End If

NewestFile = MostRecentFile

End Function

速度问题:Dir功能与FileSystemObject

就速度而言,如果您要查看的文件夹包含少量文件,则这两种方法会在大致相同的时间内为您提供相同的结果。但是,如果该文件夹中有大量文件,则使用 Dir Function 方法而不是 FileSystemObject 会大大加快宏的执行速度。我没有测试过,但这似乎是this question中答案得出的结论。