如何在excel vba中过滤datemodified的文件

我有一个代码片段,返回文件名,大小,path和date。 我怎样才能把一个if语句这样的date大于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 

喜欢这个:

 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 

为什么不在最后过滤而不是使用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