Find the string in all excel files in destination folder and subfolders

praveenpatel421983

New Member
Joined
Aug 17, 2017
Messages
41
Hi all,

I am trying to write a code to search a strings listed in the active sheet in all excel files in a folder having multi level of subfolders. I searched and found following code which is working for me but I need to search only sheet called "Test" in each excel file. This code searches in every sheet. Please help to restrict it to "Test" sheet.



Public Sub NonRecursiveMethod()
Dim FSO, oFolder, oSubfolder, oFile, queue As Collection
Application.DisplayAlerts = False
Application.ScreenUpdating = False

Set FSO = CreateObject("Scripting.FileSystemObject")
Set queue = New Collection
queue.Add FSO.GetFolder("D:\ToolTest")


Do While queue.Count > 0
Set oFolder = queue(1)
queue.Remove 1 'dequeue

For Each oSubfolder In oFolder.SubFolders
queue.Add oSubfolder 'enqueue
Sheets("Sheet1").Range("A2") = oSubfolder.path

Call LoopThroughFiles

Next oSubfolder

Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True


MsgBox "Done"


End Sub




Sub LoopThroughFiles()
Dim file As Variant
Dim sht As Worksheet, path As String, y As Range

Application.DisplayAlerts = False
Application.ScreenUpdating = False

i = 2
path = ThisWorkbook.Sheets("sheet1").Range("a2").Value
If Right(path, 1) <> "" Then path = path & ""
file = Dir(path & "*.xls*")
While (file <> "")
Workbooks.Open path & file
For Each cell In ThisWorkbook.Sheets("sheet1").Range("B4:B53")

For Each sht In ActiveWorkbook.Sheets


Set y = sht.Cells.Find(What:=cell.Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)


If y Is Nothing Then
GoTo Skip:
Else
ThisWorkbook.Sheets("sheet2").Range("a" & i).Value = cell.Value
ThisWorkbook.Sheets("sheet2").Range("b" & i).Value = y.Address
ThisWorkbook.Sheets("sheet2").Range("c" & i).Value = sht.Name
ThisWorkbook.Sheets("sheet2").Range("d" & i).Value = file
ThisWorkbook.Sheets("sheet2").Range("e" & i).Value = path
i = i + 1
End If
found = y.Address

Skip:


Next sht
Next cell
ActiveWorkbook.Close

file = Dir()
Wend
On Error GoTo 0
Application.DisplayAlerts = True
Application.ScreenUpdating = True


End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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