【问题标题】:Excel VBA Count files last modified before dateExcel VBA 计数文件最后修改日期之前
【发布时间】:2015-10-24 12:38:28
【问题描述】:

我正在寻找一些代码来计算特定文件夹中的文件数量,这些文件具有特定的最后修改日期:今天 - 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

【问题讨论】:

  • 你可以使用 FileDateTime( FILEPATH ) 吗?这应该返回创建/上次修改时的本地 PC 时区中的日期时间

标签: excel vba file count last-modified


【解决方案1】:

类似下面的东西应该可以工作:

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

【讨论】:

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