Option Explicit
Private Sub Workbook_Open()
Application.OnKey "^%F", "ShowSearchDialog"
Application.OnKey "^%f", "ShowSearchDialog"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "^%F"
Application.OnKey "^%f"
On Error Resume Next
ResetCellColor
Unload ufmSearch
End Sub
Option Explicit
Public Arr() As String
Public intCurSheet, intNextSheet, intPrevSheet As Integer
Public curCell, nextCell, prevCell As Range
Public lastColor As Variant
Sub ShowSearchDialog()
Load ufmSearch
ufmSearch.Show vbModeless
End Sub
Function NextCellExists(ByVal cell As Range) As Boolean
With Worksheets(Arr(intCurSheet))
Set nextCell = .Cells.Find(What:=ufmSearch.txtSearch.Text, After:=cell, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If Not nextCell Is Nothing And nextCell.Address <> cell.Address And _
(nextCell.Row > cell.Row Or _
(nextCell.Row = cell.Row And nextCell.Column > cell.Column)) Then
intNextSheet = intCurSheet
NextCellExists = True
Else
If intNextSheet < UBound(Arr) Then
intNextSheet = intNextSheet + 1
If NextCellInNextSheet Then NextCellExists = True
Else
ufmSearch.btnNext.Enabled = False
End If
End If
End With
End Function
Function NextCellInNextSheet() As Boolean
Dim i As Integer
For i = intNextSheet To UBound(Arr)
With Worksheets(Arr(i))
Set nextCell = .Cells.Find(What:=ufmSearch.txtSearch.Text, _
After:=.Cells(.Rows.Count, .Columns.Count), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If Not nextCell Is Nothing Then Exit For
End With
Next
If Not nextCell Is Nothing Then
intNextSheet = i
NextCellInNextSheet = True
End If
End Function
Function PreviousCellExists(ByVal cell As Range) As Boolean
With Worksheets(Arr(intCurSheet))
Set prevCell = .Cells.Find(What:=ufmSearch.txtSearch.Text, After:=cell, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False)
If Not prevCell Is Nothing And prevCell.Address <> cell.Address And _
(prevCell.Row < cell.Row Or _
(prevCell.Row = cell.Row And prevCell.Column < cell.Column)) Then
intPrevSheet = intCurSheet
PreviousCellExists = True
Else
If intPrevSheet > LBound(Arr) Then
intPrevSheet = intPrevSheet - 1
If PreviousCellInPreviousSheet Then PreviousCellExists = True
Else
ufmSearch.btnPrevious.Enabled = False
End If
End If
End With
End Function
Function PreviousCellInPreviousSheet() As Boolean
Dim i As Integer
For i = intPrevSheet To LBound(Arr) Step -1
With Worksheets(Arr(i))
Set prevCell = .Cells.Find(What:=ufmSearch.txtSearch.Text, After:=.Range("A1"), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False)
If Not prevCell Is Nothing Then Exit For
End With
Next
If Not prevCell Is Nothing Then
intPrevSheet = i
PreviousCellInPreviousSheet = True
End If
End Function
Sub ResetCellColor()
On Error Resume Next
If Not curCell Is Nothing Then curCell.Interior.ColorIndex = lastColor
End Sub
Option Explicit
Private Sub UserForm_Initialize()
Me.txtSearch.SelectionMargin = False
Me.txtSearch.TabIndex = 0
Me.btnFindFirst.TabIndex = 1
Me.btnFindFirst.TakeFocusOnClick = False
Me.btnPrevious.TabIndex = 2
Me.btnPrevious.TakeFocusOnClick = False
Me.btnNext.TabIndex = 2
Me.btnNext.TakeFocusOnClick = False
Me.btnExit.TabIndex = 3
Me.btnExit.TakeFocusOnClick = False
Me.btnPrevious.Enabled = False
Me.btnNext.Enabled = False
Me.btnFindFirst.Default = True
Me.btnExit.Cancel = True
End Sub
Private Sub UserForm_Activate()
Dim i As Integer, Sht As Worksheet
For Each Sht In Worksheets
i = i + 1
ReDim Preserve Arr(1 To i)
Arr(i) = Sht.Name
Next
Me.txtSearch.SetFocus
End Sub
Private Sub btnFindFirst_Click()
Dim i As Integer
If Trim(Me.txtSearch.Text) = "" Then Exit Sub
intCurSheet = 1
For i = intCurSheet To UBound(Arr)
With Worksheets(Arr(i))
Set curCell = .Cells.Find(What:=Me.txtSearch.Text, _
After:=.Cells(.Rows.Count, .Columns.Count), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If Not curCell Is Nothing Then Exit For
End With
Next
If Not curCell Is Nothing Then
ResetCellColor
curCell.Parent.Activate
lastColor = curCell.Interior.ColorIndex
curCell.Interior.ColorIndex = 4
Set prevCell = curCell
curCell.Select
intCurSheet = i
If NextCellExists(curCell) Then
Me.btnNext.Enabled = True
End If
Else
MsgBox "Entered string not found", , "Search complete"
End If
End Sub
Private Sub btnNext_Click()
ResetCellColor
Set prevCell = curCell
intPrevSheet = intCurSheet
Set curCell = nextCell
intCurSheet = intNextSheet
curCell.Parent.Activate
lastColor = curCell.Interior.ColorIndex
curCell.Interior.ColorIndex = 4
curCell.Select
Me.btnPrevious.Enabled = True
Me.btnNext.Enabled = IIf(NextCellExists(curCell), True, False)
End Sub
Private Sub btnPrevious_Click()
ResetCellColor
Set nextCell = curCell
intNextSheet = intCurSheet
Set curCell = prevCell
intCurSheet = intPrevSheet
curCell.Parent.Activate
lastColor = curCell.Interior.ColorIndex
curCell.Interior.ColorIndex = 4
curCell.Select
Me.btnNext.Enabled = True
Me.btnPrevious.Enabled = IIf(PreviousCellExists(curCell), True, False)
End Sub
Private Sub txtSearch_Change()
Me.btnNext.Enabled = False
Me.btnPrevious.Enabled = False
End Sub
Private Sub btnExit_Click()
ResetCellColor
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then ResetCellColor
End Sub
|