【问题标题】:Excel Macro to find last modified date of fileExcel宏查找文件的最后修改日期
【发布时间】:2016-09-06 11:43:12
【问题描述】:

有人可以帮助我使用 Excel VBA 宏来搜索 B 列中提供的各种目录中的文件,基于 A 列中给出的关键字并在 C 列中返回“文件存在”/“文件不存在”并返回最后一个D列中文件的修改日期和时间。

例子

Keyword | FolderPath        | Result / last modified date & time 
--------+-------------------+-----------------------------------------
1234    | E:\Documents\ABC  |

我是 Excel 宏的新手。请帮帮我!

提前致谢!

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    使用这个..

    FileDateTime(Path as String)
    

    更多详情访问 https://www.techonthenet.com/excel/formulas/filedatetime.php

    【讨论】:

      【解决方案2】:

      要使用宏,请按照以下步骤操作:

      1. Alt+F11 => 打开 VBA IDE
      2. 从菜单栏Insert > Module => 将模块添加到您的 Excel 中
      3. 编写宏代码。

      你可以使用这个宏:

      Sub UpdateFileDate()
      
        Dim i As Long
        Dim strTemp As String
        Dim fso As Object
        Dim fileTemp As Object
        Dim strDate As Date
      
        ' Open library of Microsoft Scripting Runtime
        Set fso = CreateObject("Scripting.FileSystemObject")
      
        For i = 1 To ActiveSheet.Rows.Count
           strTemp = Trim(ActiveSheet.Cells(i, 2).Value & " ")
           If (strTemp = "") Then Exit For
      
           If (fso.FolderExists(strTemp)) Then
      
              ' Set a min value to strDate
              strDate = DateTime.DateSerial(-1000, 1, 1)
      
              ' Check All files in the folder
              For Each fileTemp In fso.GetFolder(strTemp).Files
                 If (strDate < fileTemp.DateLastModified) Then
                     strDate = fileTemp.DateLastModified
                 End If
              Next
      
              If (strDate <> DateTime.DateSerial(-1000, 1, 1)) Then
                  ActiveSheet.Cells(i, 3).Value = CStr(strDate)
              Else
                  ActiveSheet.Cells(i, 3).Value = "No File"
              End If
           End If
        Next i
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-20
        • 1970-01-01
        • 1970-01-01
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多