hippyrider
New Member
Hi experts
I have been trying to modify the below to search all workbooks in a specific folder but restrict the search to the IndexPage sheet not all the sheets. Any ideas my "If wks.Name = "IndexPage" Then" does not speed it up at all.
I have been trying to modify the below to search all workbooks in a specific folder but restrict the search to the IndexPage sheet not all the sheets. Any ideas my "If wks.Name = "IndexPage" Then" does not speed it up at all.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
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
'Change as desired
strPath = "D:\shops\Stats"
strSearch = Range("A1").Value
Set wOut = Worksheets.Add
lRow = 1
With wOut
.Cells(lRow, 1) = "Link"
.Cells(lRow, 2) = "Workbook"
.Cells(lRow, 3) = "Worksheet"
.Cells(lRow, 4) = "Cell"
.Cells(lRow, 5) = "Text in Cell"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(strPath)
strFile = Dir(strPath & "\*.xlsm*")
Do While strFile <> ""
Set wbk = Workbooks.Open _
(Filename:=strPath & "\" & strFile, _
UpdateLinks:=0, _
ReadOnly:=True, _
AddToMRU:=False)
For Each wks In wbk.Worksheets
If wks.Name = "IndexPage" Then
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) = "=HYPERLINK(""[D:\shops\Stats\" & wbk.Name & "]ChartView!A1"",""CLICK HERE"")"
.Cells(lRow, 2) = wbk.Name
.Cells(lRow, 3) = wks.Name
.Cells(lRow, 4) = rFound.Address
.Cells(lRow, 5) = rFound.Value
End If
Set rFound = wks.Cells.FindNext(After:=rFound)
Loop While strFirstAddress <> rFound.Address
End If
Next
wbk.Close (False)
strFile = Dir
Loop
.Columns("A:E").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 If