I have a sub that's part of a larger routine to search folders and subfolders. I want to integrate a script that once a subfolder is found, to look within the subfolder and find files that contain the letters *dq* within it. I've attempted to try to use a couple approaches and have come up with this as my best option, although when I run it, no errors are thrown and no output occurs even though there are numerous files within the main directory meeting the criteria. My objective is when a file is found within the subfolder to open and search for a string and if true then list the file name and location in the sheet, then proceed to the next file within the subfolder.
Sub ListFiles(ByRef Folder As Object)
Dim fName as String
If Not Folder Like "*dq*" Then Exit Sub
Set sht = ThisWorkbook.Worksheets("Sheet1")
r = sht.Cells(sht.Rows.count, "A").End(xlUp).Row + 1
With ActiveSheet
On Error Resume Next
For Each File In Folder.Files
fName = File.Name
With File
If fName Like "*dq*" Then
'open the file and call the string search
'if string search then
.Cells(r, 1).Value = File.ParentFolder
.Cells(r, 2).Value = File.ShortName
.Cells(r, 3).Value = File.ShortPath
.Cells(r, 4).Value = File.Type
r = r + 1
End If
End With
Next File
End With
End Sub
EDIT: after looking at the code, I realized I had a redundant search. I revised to look at each File within the loop, however it still does not find any existing files within the directory
Bookmarks