Search Files in Subfolders
Search Files in Subfolders
multiple Excel files stored in a selected parent folder and all its subfolders. It
begins by prompting the user to choose a parent directory and enter the search
text. A new worksheet is then created to store the results, including the workbook
name, worksheet name, cell location, and the text found.
The script uses a FileSystemObject to navigate through all subfolders and identify
Excel files with extensions .xls, .xlsx, and .xlsm. For each file, it opens the
workbook in read-only mode and searches through all sheets using the Find method to
locate multiple occurrences of the specified text. The results are recorded
dynamically in the output worksheet.
To ensure efficiency and prevent infinite loops, the script properly handles the
FindNext function, ensuring that all instances of the search text are captured.
Once all files have been scanned, the workbooks are closed without saving any
changes, and the results sheet is formatted for better readability. The script
includes error handling mechanisms and automatic cleanup of objects to prevent
memory leaks. Upon completion, a message box notifies the user that the search is
finished. This makes the script an efficient tool for searching large numbers of
Excel files spread across multiple folders.
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------------------------------------------
Key Features:
✅ Processes all subfolders within the selected directory.
✅ Works with multiple Excel file formats (.xls, .xlsx, .xlsm).
✅ Handles multiple occurrences of the search text in a worksheet.
✅ Prevents infinite loops using FindNext logic.
✅ Error handling and cleanup ensure smooth execution.
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------------------------------------------
Sub SearchFilesInSubfolders()
Dim fileSystem As Object
Dim searchText As String
Dim folderPath As String
Dim outputSheet As Worksheet
Dim lastRow As Long
Dim folderDialog As FileDialog
If folderDialog.Show = -1 Then
folderPath = folderDialog.SelectedItems(1)
Else
MsgBox "No folder selected. Exiting.", vbExclamation
Exit Sub
End If
With outputSheet
' Add headers to the result sheet
.Cells(lastRow, 1) = "Workbook"
.Cells(lastRow, 2) = "Worksheet"
.Cells(lastRow, 3) = "Cell"
.Cells(lastRow, 4) = "Text in Cell"
.Cells(lastRow, 5) = "Link to Cell"
ExitProcedure:
' Cleanup
Set outputSheet = Nothing
Set fileSystem = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Exit Sub
ErrorHandler:
MsgBox "Error: " & Err.Description, vbExclamation
Resume ExitProcedure
End Sub
lastRow = lastRow + 1
outputSheet.Cells(lastRow, 1) = currentWorkbook.Name
outputSheet.Cells(lastRow, 2) = currentSheet.Name
outputSheet.Cells(lastRow, 3) = foundCell.Address
outputSheet.Cells(lastRow, 4) = foundCell.Value