【问题标题】:How to import in excel the path of the last file saved?如何在excel中导入最后保存的文件的路径?
【发布时间】:2016-05-12 14:17:53
【问题描述】:

我想导入文件 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

【问题讨论】:

  • 您的意思是要导入给定文件夹结构(包括子文件夹)中所有文件的文件路径并将它们写入电子表格的 A 列?
  • 相信你可以在这里找到解决办法:stackoverflow.com/questions/17847457/…

标签: vba excel


【解决方案1】:

试试这个:

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

【讨论】:

  • 谢谢!!但这会导入最后一个文件。如果我们在同一天导入了很多文件?请问我们能做什么?我被卡住了:s
  • 如果您在同一天导入了许多文件,它也可以工作。
  • 我将不胜感激。
  • 你可以在答案中测试它的代码,我已经改变了!
  • 谢谢!这是完美的!
猜你喜欢
  • 2014-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多