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

时间:2015-10-24 12:38:29

标签: excel vba file count last-modified

我正在寻找一些代码来计算特定文件夹中的文件数量,这些文件具有特定的上次修改日期:今天 - 90.

我确实编写了一个代码来计算文件夹中的所有文件(这是我想要的一部分),但是当文件较旧时我会被计算在内。

任何建议都非常感谢!

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

1 个答案:

答案 0 :(得分:0)

以下内容应该有效:

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