如何在excel中导入保存的最后一个文件的路径?

时间:2016-05-12 14:17:54

标签: excel vba excel-vba

我想导入文件C:\ Users \ fld的整个路径,但仅导入今天的最近文件。

你能帮我吗?

在我的vba下面导入路径:

Sub test()
      Dim fso As FileSystemObject
      Dim oSourceFolder As Scripting.Folder
      Dim oSubFolder As Scripting.Folder
      Dim oFile As Scripting.File
      Dim oFolder As Scripting.Folder
      Dim strFolderName As String
      Dim i As Long
        Set fso = CreateObject("Scripting.FileSystemObject")
        Cells(1, 1).Value = "fld"   
       strFolderName= "C:\Users\fld"

        i = 2      
        Range("A2").Select
        ActiveCell = Range("A2")   
      Set oSourceFolder = fso.GetFolder(strFolderName)
      For Each oFolder In oSourceFolder.SubFolders
                For Each oFile In oFolder.Files
                Cells(i, 1).Value = Left(oFile.ParentFolder)                
                             i = i + 1
                Next oFile
        Next oFolder

1 个答案:

答案 0 :(得分:0)

试试这个:

Sub test()
    Dim fso As FileSystemObject
    Dim oSourceFolder As Scripting.Folder
    Dim oSubFolder As Scripting.Folder
    Dim oFile As Scripting.File

    Dim oFolder As Scripting.Folder
    Dim strFolderName As String
    Dim i As Long
    Set fso = CreateObject("Scripting.FileSystemObject")
    strFolderName = "C:\Users\fld"
    i = 2
    Range("A2").Select
    ActiveCell = Range("A2")

    Dim dt As Date
    Dim pathLastUpdatePath As String

    'Find last Date of file----------------------------------------'
    'Find file that have last DateTime Update in parent folder
    Set oFolder = fso.GetFolder(strFolderName)
    For Each oFile In oFolder.Files
        fileModDate = oFile.DateLastModified
        If fileModDate > dt Then
            dt = fileModDate
            pathLastUpdatePath = oFile.Path
        End If
    Next oFile

    'Find file that have last DateTime Update in Sub folders
    Set oSourceFolder = fso.GetFolder(strFolderName)
    For Each subFolder In oSourceFolder.SubFolders
        For Each sFile In subFolder.Files
            fileModDate = sFile.DateLastModified
            If fileModDate > dt Then
                dt = fileModDate
                pathLastUpdatePath = sFile.Path
            End If
        Next sFile
    Next subFolder

    'Find and Show all files are modified in the same day----------------------'
    Cells(1, 1).Value = "File with DateLastModified"
    Cells(1, 2).Value = "DateTime File"

    Set oFolder = fso.GetFolder(strFolderName)
    For Each oFile In oFolder.Files
        fileModDate = oFile.DateLastModified
        If DateValue(fileModDate) = DateValue(dt) Then
            Cells(i, 1).Value = oFile.Path
            Cells(i, 2).Value = fileModDate
            i = i + 1
        End If
    Next oFile

    Set oSourceFolder = fso.GetFolder(strFolderName)
    For Each subFolder In oSourceFolder.SubFolders
        For Each sFile In subFolder.Files
            fileModDate = sFile.DateLastModified
            If DateValue(fileModDate) = DateValue(dt) Then
                Cells(i, 1).Value = sFile.Path
                Cells(i, 2).Value = fileModDate
                i = i + 1
            End If
        Next sFile
    Next subFolder
End Sub