Sorting dates

×

×לי

Hi!

I have found a function that imports a list of file names with its last
modification date (www.exceltip.com) and I have modified it so only files
with *.res extention will be imported. I want also to import only files that
was modified in the last 2 days but it not worked (see the code below). the
date is in the format of: dd/mm/yyyy hh:mm.

Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As
Boolean)
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
Dim FileItem As Scripting.File
Dim r As Long
Dim t As Long
Dim s As Date
Set FSO = New Scripting.FileSystemObject
Set SourceFolder = FSO.GetFolder(SourceFolderName)
s = Now()
r = Range("A65536").End(xlUp).Row + 1
For Each FileItem In SourceFolder.Files
t = s - FileItem.DateLastModified
If Right(FileItem.Name, 3) = "res" And t < 2 Then
Cells(r, 1).Formula = FileItem.Name
Cells(r, 2).Formula = FileItem.DateLastModified
End If
r = r + 1 ' next row number
Next FileItem
Columns("A:B").AutoFit
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
ActiveWorkbook.Saved = True
End Sub

I will be happy to understand what is wrong with it.

Thanks in advance

Eli
 
B

Bernie Deitrick

Eli,

Your code works fine, but you may have not seen anything because you increment your row counter
whether or not the file meets your requirement, so change

End If
r = r + 1 ' next row number

to

r = r + 1 ' next row number
End If

HTH,
Bernie
MS Excel MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top