我似乎无法弄清楚如何通过子文件夹进行当前代码搜索。代码当前采用列A并在目录和子文件夹中搜索与单元格匹配的文件名,并将文件名放在列B中,将文件路径放在列C中。我只是希望它能够搜索子文件夹以找到A列中指定的文件。
Sub due() 'Checks for filenames with criteria listed in column A, return full name and file path.
Dim sh As Worksheet, rng As Range, lr As Long, fPath As String
Set sh = Sheets(1)
lstRw = sh.Cells.Find(What:="*", After:=sh.Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
Set rng = sh.Range("A1:A" & lstRw)
fPath = "C:\Users\DZ82CZ\Documents\Test" 'File Path
If Right(fPath, 1) <> "\" Then
fPath = fPath & "\"
End If
Set newSh = Sheets.Add(After:=Sheets(Sheets.Count))
fWb = Dir(fPath & "*.*")
x = 2
Do While fWb <> ""
For Each C In rng
If InStr(LCase(fWb), LCase(C.Value)) > 0 Then
newSh.Range("A" & x) = C.Value
newSh.Range("B" & x) = fWb
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(fWb)
newSh.Range("C" & x) = f.Path
Columns("B:C").AutoFit
Set fs = Nothing
Set f = Nothing
x = x + 1
End If
Next
fWb = Dir
Loop
Set newSh = Nothing
Set sh = Nothing
Set rng = Nothing
End Sub