Option Explicit
Sub QuickSearch()
Dim wks As Excel.Worksheet
Dim rCell As Excel.Range
Dim szFirst As String
Dim i As Long
Dim bTag As Boolean
bTag = True
Dim szLookupVal As String
szLookupVal = InputBox("What are you searching for", "Search-Box", "")
If szLookupVal = "" Then Exit Sub
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each wks In ActiveWorkbook.Worksheets
If wks.Name = "Found it here" Then
wks.Delete
End If
Next wks
Sheets.Add ActiveSheet
ActiveSheet.Name = "Found it here"
With Cells(1, 1)
.Value = "Found " & szLookupVal & " in the cells below:"
.EntireColumn.AutoFit
.HorizontalAlignment = xlCenter
End With
ActiveSheet.Next.Select
If MsgBox("Would you like to highlight all found occurences also?", vbYesNo, _
"Highlight Cells") = vbNo Then
bTag = False
End If
i = 2
For Each wks In ActiveWorkbook.Worksheets
With wks.Cells
Set rCell = .Find(szLookupVal, , , xlWhole, xlByColumns, xlNext, False)
If Not rCell Is Nothing Then
szFirst = rCell.Address
Do
rCell.Hyperlinks.Add Sheets("Found it here").Cells(i, 1), "", "'" & wks.Name & "'!" & rCell.Address
Select Case bTag
Case True
rCell.Interior.ColorIndex = 19
End Select
Set rCell = .FindNext(rCell)
i = i + 1
Loop While Not rCell Is Nothing And rCell.Address <> szFirst
End If
End With
Next wks
Set rCell = Nothing
If i = 2 Then
MsgBox "The value {" & szLookupVal & "} was not found on any sheet", 64, "No Matches"
Sheets("Found It Here").Delete
End If
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
|