Excel宏以查找文件的上次修改日期

时间:2014-04-18 05:32:27

标签: excel-vba vba excel

根据A列中给出的关键字并返回"文件存在" /"文件不是,有人可以帮我用Excel VBA宏来搜索B列中提供的各种目录中的文件本"在C列中,返回D列中文件的最后修改日期和时间。

实施例

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

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

提前致谢!

2 个答案:

答案 0 :(得分:0)

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

  1. Alt + F11 =>打开VBA IDE
  2. 从菜单栏Insert > Module =>将模块添加到Excel
  3. 编写宏代码。
  4. 您可以使用此宏:

    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
    

答案 1 :(得分:0)

使用此..

FileDateTime(Path as String)

更多细节访问 https://www.techonthenet.com/excel/formulas/filedatetime.php