Hi,
I have found the following and modified it a bit but need to modify it a little more for my needs. This allows the user to enter the search criteria and folder to be searched. It then goes through all files in that folder and identifies the files that contain the criteria being searched. I would like to only search in a specified worksheet ("EM") in each workbook. In addition I need to add to the output. Currently this is creating a new tab with four data items. I would also like to return another specified cell from the same row in which the specified data was found. This cell will always be 6 columns to the right of the Found.Address.
Thanks in advance for the help!
I have found the following and modified it a bit but need to modify it a little more for my needs. This allows the user to enter the search criteria and folder to be searched. It then goes through all files in that folder and identifies the files that contain the criteria being searched. I would like to only search in a specified worksheet ("EM") in each workbook. In addition I need to add to the output. Currently this is creating a new tab with four data items. I would also like to return another specified cell from the same row in which the specified data was found. This cell will always be 6 columns to the right of the Found.Address.
Thanks in advance for the help!
Rich (BB code):
Sub SearchFolders()
Dim Fso As Object
Dim Fld As Object
Dim RngSearch As Range
Dim StrPath As String
Dim StrFile As String
Dim Out As Worksheet
Dim Wb As Workbook
Dim Wk As Worksheet
Dim Row As Long
Dim Found As Range
Dim StrAddress As String
Dim FileDialog As FileDialog
Dim Update As Boolean
Dim Count As Long
On Error GoTo ErrHandler
Set FileDialog = Application.FileDialog(msoFileDialogFolderPicker)
FileDialog.AllowMultiSelect = False
FileDialog.Title = "Select a forlder"
If FileDialog.Show = -1 Then
StrPath = FileDialog.SelectedItems(1)
End If
If StrPath = "" Then Exit Sub
Set RngSearch = ActiveWorkbook.Worksheets("Sheet1").Range("B3:E5")
Update = Application.ScreenUpdating
Application.ScreenUpdating = False
Set Out = Worksheets.Add
Row = 1
With Out
.Cells(Row, 1) = "Workbook"
.Cells(Row, 2) = "Worksheet"
.Cells(Row, 3) = "Cell Address"
.Cells(Row, 4) = "Search Criteria"
.Cells(Row, 5) = "QLE Date"
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Fld = Fso.GetFolder(StrPath)
StrFile = Dir(StrPath & "\*.xls*")
Do While StrFile <> ""
Set Wb = Workbooks.Open(Filename:=StrPath & "" & StrFile, UpdateLinks:=0, ReadOnly:=True, AddToMRU:=False)
For Each Wk In Wb.Worksheets
Set Found = Wk.UsedRange.Find(RngSearch)
If Not Found Is Nothing Then
StrAddress = Found.Address
End If
Do
If Found Is Nothing Then
Exit Do
Else
Count = Count + 1
Row = Row + 1
.Cells(Row, 1) = Wb.Name
.Cells(Row, 2) = Wk.Name
.Cells(Row, 3) = Found.Address
.Cells(Row, 4) = Found.Value
.Cells(Row, 5) = ????
End If
Set Found = Wk.Cells.FindNext(After:=Found)
Loop While StrAddress <> Found.Address
Next
Wb.Close (False)
StrFile = Dir
Loop
.Columns("A:D").EntireColumn.AutoFit
End With
MsgBox Count & "cells have been found"
ExitHandler:
Set Out = Nothing
Set Wk = Nothing
Set Wb = Nothing
Set Fld = Nothing
Set Fso = Nothing
Application.ScreenUpdating = Update
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
Last edited by a moderator: