I have tried to write code to list files on sheet "Macro" containing "TB" in C:\My documents for eg bR1TB (Nert).xls . SouternTB.xls etc within a speciied date range in D2:E2 (Sheet Macro)
These last date modified was 07/03/2023 (format dd/mm/yyyy)
However when running the macro no files have been extracted
It would be appreciated if someone could kindly amend my code
Sub ListFiles()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim strSearch As String
Dim strFolder As String
'Set the folder path to search
strFolder = "C:\my documents\"
'Set the search string to look for
strSearch = "*TB*"
'Set up the file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
'Loop through the files in the folder and add to the list if they match the search string and fall within the date range
i = 1
For Each objFile In objFolder.Files
If InStr(1, objFile.Name, strSearch, vbTextCompare) > 0 And _
objFile.DateLastModified >= Range("D2").Value And _
objFile.DateLastModified <= Range("E2").Value Then
Range("C" & i).Value = objFile.Name
i = i + 1
End If
Next objFile
'Clean up the objects
Set objFile = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
End Sub
Bookmarks