Excel VBA计数文件最后在date之前修改

我正在寻找一些代码,计算特定的文件夹中的文件数量,具有特定的最后修改date:今天 – 90。

我做了代码来计算一个文件夹中的所有文件(这是我想要的一部分),但是当文件比较老时,卡住了计数。

任何build议不胜感激!

Sub CountFiles() Application.ScreenUpdating = False Application.DisplayAlerts = False 'Set the paths Dim PathEvaluations As String Dim PathPDF As String Dim PathA As String Dim PathB As String Dim CountEvaluations As Integer Dim CountOldEvals As Integer Dim CountPDF As Integer Dim CountOldPDF As Integer Dim MsgBoxTitle As String Dim PurgeDate As Date PathEvaluations = Worksheets("References").Range("B50").Value PathPDF = Worksheets("References").Range("B51").Value MsgBoxTitle = Worksheets("References").Range("B32").Value PurgeDate = Worksheets("References").Range("B77").Value PathA = PathEvaluations & "*.xlsx" Filename = Dir(PathA) Do While Filename <> "" CountEvaluations = CountEvaluations + 1 Filename = Dir() Loop PathB = PathPDF & "*.pdf" Filename = Dir(PathB) Do While Filename <> "" CountPDF = CountPDF + 1 Filename = Dir() Loop MsgBox "System maintenance:" & vbNewLine & vbNewLine & _ CountEvaluations & " files found in: evaluations folder" & vbNewLine & _ "of which " & CountOldEvals & " are from before: " & PurgeDate & " and can be deleted!" & vbNewLine & vbNewLine & _ CountPDF & " files found in: pdf folder" & vbNewLine & _ "of which " & CountOldPDF & " are from before: " & PurgeDate & " and can be deleted!", vbInformation, MsgBoxTitle End Sub 

像下面的东西应该工作:

 Dim FileDate As Date Dim Minus90 As Date Minus90 = DateAdd("d", -90, Date) PathA = PathEvaluations & "*.xlsx" Filename = Dir(PathA) Do While Filename <> "" CountEvaluations = CountEvaluations + 1 FileDate = FileDateTime(PathEvaluations & Filename) If FileDate <= Minus90 Then CountOldEvals = CountOldEvals + 1 End If MsgBox (FileDate) Filename = Dir() Loop