2

我有一个返回文件名、大小、路径和日期的代码片段。如何放置 if 语句以便显示大于 X 的日期?

    Dim irow
Sub ListFiles()
    irow = 11
    Call ListMyFiles(Range("C7"), Range("C8"))
End Sub

Sub ListMyFiles(mySourcePath, IncludeSubfolders)
    Set MyObject = New Scripting.FileSystemObject
    Set mySource = MyObject.GetFolder(mySourcePath)
    On Error Resume Next
    For Each myFile In mySource.Files
            iCol = 2
            Cells(irow, iCol).Value = myFile.Path
            iCol = iCol + 1
            Cells(irow, iCol).Value = myFile.Name
            iCol = iCol + 1
            Cells(irow, iCol).Value = myFile.Size
            iCol = iCol + 1
            Cells(irow, iCol).Value = myFile.DateLastModified
            irow = irow + 1
    Next
    If IncludeSubfolders Then
        For Each mySubFolder In mySource.SubFolders
            Call ListMyFiles(mySubFolder.Path, True)
        Next
    End If
End Sub
4

2 回答 2

0

像这样:

Dim myDate as Date
myDate = "12.02.1985"

For Each myFile In mySource.Files
    if myFile.DateLastModified > myDate then
        iCol = 2
        Cells(irow, iCol).Value = myFile.Path
        iCol = iCol + 1
        Cells(irow, iCol).Value = myFile.Name
        iCol = iCol + 1
        Cells(irow, iCol).Value = myFile.Size
        iCol = iCol + 1
        Cells(irow, iCol).Value = myFile.DateLastModified
        irow = irow + 1
    end if
Next
于 2012-09-26T15:47:06.443 回答
0

为什么不在最后过滤而不是使用IF

Option Explicit
Sub FilterbyDate()
    Dim ws As Worksheet
    Dim rng As Range
    Dim frng As Range
    Set ws = Sheets("Sheet1")
    ' Remove any previous filters
    ws.AutoFilterMode = False
    ' Specify range to be filtered
    Set rng = ws.Range("xx")

    With rng
        ' Filter data by date:
        ' where field is the column number with your dates and
        ' change the date criteria to what you desire
        .AutoFilter Field:=1, Criteria1:=">dd/mm/yyyy hh:mm:ss"
        ' Define the filtered range;
        ' You could then copy it to a new sheet
        ' deleting original so you're only left
        ' with the data you want
         Set frng = .SpecialCells(xlCellTypeVisible)
        ' frng.Copy
    End With
End Sub
于 2012-09-26T15:48:14.463 回答