Hello all! I have VBA code in a macro that searches for text in multiple worksheets in workbooks and all folders and creates a new worksheet with 4 columns. The 4 columns returned are workbook Name, Worksheet Name, Cell Address and Text Found. In the VBA code (screeshot1), strPath is the path where my folder "Data Flow Analysis Tracker" is located and it has multiple workbooks which I want to search for particular text. When I use the strPath in the code, I'm getting the error message in the code highlighted in yellow SCREENSHOT1). But when i change the strPath to a folder that does not exist i.e strPath = "C:\MyExcelData", the code seems to work only the folder specified does not exist.
If anyone can help me fix this, I will be very greatful. The full vba code is below as well as screenshots
SCREENSHOT1
SCREENSHOT2
If anyone can help me fix this, I will be very greatful. The full vba code is below as well as screenshots
VBA Code:
Sub SearchAllFolders()
Dim strSearch As String
Dim strPath As String
Dim strFile As String
Dim wksReport As Worksheet
Dim wbk As Workbook
Dim wks As Worksheet
Dim StartRow As Long
Dim ringFound As Range
Dim strFirstAddress As String
Application.ScreenUpdating = False
strPath = "C:\MyExcelData"
strSearch = Application.InputBox("Enter the text to search", "My choice of text", "Enter Search Text", vbOKCancel)
If strSearch = "False" Then Exit Sub
Set wksReport = Worksheets.Add
StartRow = 1
With wksReport
.Cells(StartRow, 1) = "Workbook"
.Cells(StartRow, 2) = "Worksheet"
.Cells(StartRow, 3) = "Cell Address"
.Cells(StartRow, 4) = "Test Found"
strFile = Dir(strPath & "\*.xls*")
Do While strFile <> ""
Set wbk = Workbookd.Open(Filename:=strPath & "\" & strFile)
For Each wks In wbk.worksheeks
Set ringFound = wks.UsedRange.Find(strSearch)
If Not ringFound Is Nothing Then
strFirstAddress = ringFound.Address
End If
Do
If ringFound Is Nothing Then
Exit Do
Else
StartRow = StartRow + 1
.Cells(StartRow, 1) = wbk.Name
.Cells(StartRow, 2) = wks.Name
.Cells(StartRow, 3) = ringFound.Address
.Cells(StartRow, 4) = ringFound.Value
End If
Set ringFound = wks.Cells.FindNext(after:=ringFound)
Loop While strFirst <> ringFound.Address
Next
wkb.Cose (False)
strFile = Dir
Loop
Columns("A:D").EntireColumn.AutoFit
End With
If wksReport.Cells(2, 1) = "" Then
MsgBox "All Excel Files In Folder Searched!" & vbCrLf & "No data found!"
Cells(StartRow, 1) = " "
Cells(StartRow, 2) = " "
Cells(StartRow, 3) = " "
Cells(StartRow, 4) = " "
On Error Resume Next
Application.DisplaysAlert = False
wksReportr.Delete
Else
MsgBox "All Excel files in folder searched. " & vbCrLf & "Data Extracted."
End If
Set wksReport = Nothing
Set wks = Nothing
Set wkb = Nothing
Application.ScreenUpdating = True
Application.DisplaysAlerts = True
Range("B19").Select
Sheets("Step 1- data flow analysis").Select
Range("A8").Select
ActiveCell.FormulaR1C1 = "Probability of Default"
End Sub
SCREENSHOT1
SCREENSHOT2
Last edited by a moderator: