Sub SearchAndRecordResults()
Dim targetStr As String
Dim folderPath As String
Dim fileName As String
Dim ws As Worksheet
Dim cell As Range
Dim resultRow As Long
' Get the target string to search for 单元格B2检索内容
targetStr = ThisWorkbook.Sheets("SEARCH").Range("B2").Value
' Get the folder path 单元格B1路径
folderPath = ThisWorkbook.Sheets("SEARCH").Range("B1").Value
' Check if the folder path exists 确认路径是否存在
If Dir(folderPath, vbDirectory) = "" Then
MsgBox "Folder path does not exist", vbExclamation
Exit Sub
End If
Dim fileNum
' Initialize the row number for recording results 第一条结果记录的起始行数
resultRow = 6
' Loop through Excel files in the folder 当前文件夹下的所有文件(*.xls*),屏蔽搜索文件本身
fileName = Dir(folderPath & "\*.xls*")
'对于如何遍历文件目录下的所有子目录请参照下面的方法遍历子文件夹“VBA 调用CMD的tree命令遍历文件”,本文只满足当前文件目录的查找