VBA to search multiple values and multiple workbooks and return filename where found

AggyRJ

New Member
Joined
Mar 29, 2013
Messages
16
I am attempting to create a tool where I input a list of values (20-25 as I don't want the code to take forever) and then Excel does a search across multiple workbooks and for each value in the list and returns what workbook it is located in and where it is within that workbook.

On another page I found a code that mostly does this, however it has a few limitations. I will post it below but this is what it does:
1. Defines the path that contains all of the workbooks to be searched
2. Defines what I am searching for
3. Creates a new worksheet with all of the search results which tells me the name of the workbook, the worksheet where it is found, and which cell it is in.

I need to add a few key abilities to this:
1. Have the path change to sort through multiple folder directories (I was thinking of typing out a list of all possible directories and having it loop through each one)
2. Search for more than one value (again I was thinking of limiting it to a list of maybe 20-25 values)
3. Add the folder directory (this is crucial as the file names themselves are all the same, they are just located in a different folder).

So. to summarize:
1. I am trying to search multiple values one at a time (from a list I will type in each time so the entire list will change each time I perform the search);
2. Through multiple folders (from a list that I type in that will not change);
3. Have a list returned of the folder where the workbook is located, the workbook name, the sheet name, and where the cell it is located at.

One last thing - I am not married to this code so if someone has something better then by all means please tell me. I am pasting what I found below.

Sub SearchFolders()
Dim fso As Object
Dim fld As Object
Dim strSearch As String
Dim strPath As String
Dim strFile As String
Dim wOut As Worksheet
Dim wbk As Workbook
Dim wks As Worksheet
Dim lRow As Long
Dim rFound As Range
Dim strFirstAddress As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False

strPath = "\\RETUS379-NT0002\ROJOS4$\Desktop\TEST"
strSearch = "2615843"

Set wOut = Worksheets.Add
lRow = 1
With wOut
.Cells(lRow, 1) = "Workbook"
.Cells(lRow, 2) = "Worksheet"
.Cells(lRow, 3) = "Cell"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(strPath)
strFile = Dir(strPath & "\*.xls*")
Do While strFile <> ""
Set wbk = Workbooks.Open _
(Filename:=strPath & "" & strFile, _
UpdateLinks:=0, _
ReadOnly:=True, _
AddToMRU:=False)
For Each wks In wbk.Worksheets
Set rFound = wks.UsedRange.Find(strSearch)
If Not rFound Is Nothing Then
strFirstAddress = rFound.Address
End If
Do
If rFound Is Nothing Then
Exit Do
Else
lRow = lRow + 1
.Cells(lRow, 1) = wbk.Name
.Cells(lRow, 2) = wks.Name
.Cells(lRow, 3) = rFound.Address
End If
Set rFound = wks.Cells.FindNext(After:=rFound)
Loop While strFirstAddress <> rFound.Address
Next
wbk.Close (False)
strFile = Dir
Loop
.Columns("A:D").EntireColumn.AutoFit
End With
MsgBox "Done"
ExitHandler:
Set wOut = Nothing
Set wks = Nothing
Set wbk = Nothing
Set fld = Nothing
Set fso = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top