'##################
'standard module code
Dim firstAddress As String, lastaddress As String
Dim ctrlStatus As CommandBarButton
Dim ctrlFN As CommandBarButton
Dim ctrlText As CommandBarControl
Dim iFindWhole As Integer
Sub BuildToolBar()
'call from WorkBook_Open
Dim TBar As CommandBar
Dim btnNew As CommandBarControl
'delete any existing instances of the toolbar
DeleteToolbar
' Add a new toolbar
Set TBar = CommandBars.Add(Name:="Worksheet Find")
TBar.Visible = True
' Add text control
Set btnNew = TBar.Controls.Add(Type:=msoControlEdit)
With btnNew
.Caption = "Find..."
.TooltipText = "Enter search string..."
.Style = msoComboLabel
.OnAction = "FindText"
End With
Set ctrlText = btnNew
' add a find next button
Set btnNew = TBar.Controls.Add(Type:=msoControlButton)
With btnNew
.Caption = "Find &next"
.TooltipText = "Find next"
.Style = msoButtonCaption
.OnAction = "FindNext"
.Enabled = False
End With
Set ctrlFN = btnNew
' add whole/part
Set btnNew = TBar.Controls.Add(Type:=msoControlButton)
With btnNew
'.BeginGroup = True
.Caption = "Match"
.TooltipText = "Match entire cell contents"
.Style = msoButtonIconAndCaption
.OnAction = "ToggleFindWhole"
iFindWhole = 1
.FaceId = 1907
End With
' add a status button
Set btnNew = TBar.Controls.Add(Type:=msoControlButton)
With btnNew
.Caption = ""
.TooltipText = "Result..."
.Style = msoButtonIconAndCaption
.Width = 240
End With
Set ctrlStatus = btnNew
'force the toolbar to 2 rows and lock
With TBar
.Height = btnNew.Height * 3
.Protection = msoBarNoResize
End With
Set btnNew = Nothing
Set TBar = Nothing
End Sub
Sub DeleteToolbar()
'call from Workbook_BeforeClose
Dim cb As CommandBar
For Each cb In CommandBars
If cb.Name = "Worksheet Find" Then
cb.Delete
End If
Next cb
End Sub
Sub FindText()
'excecute the first find
Dim c As Range
Set c = ActiveSheet.Cells.Find(What:=ctrlText.Text, _
LookIn:=xlValues, LookAt:=iFindWhole)
If Not c Is Nothing Then
'activate the found cell, capture address,
'enable find next button, update status
c.Activate
ctrlFN.Enabled = True
firstAddress = c.Address
lastaddress = c.Address
ctrlStatus.Caption = """" & ctrlText.Text & """" & " found at " & lastaddress
ctrlStatus.FaceId = 1087
Else
'disable Find next, clear addresses, update status
ctrlFN.Enabled = False
firstAddress = ""
lastaddress = ""
ctrlStatus.Caption = """" & ctrlText.Text & """" & " not found"
ctrlStatus.FaceId = 1088
End If
Set c = Nothing
End Sub
Sub FindNext()
Dim c As Range
Set c = ActiveSheet.Cells.FindNext(ActiveSheet.Range(lastaddress))
If c.Address <> firstAddress Then
'activate the found cell, update address and status
c.Activate
lastaddress = c.Address
ctrlStatus.Caption = """" & ctrlText.Text & """" & " found at " & lastaddress
ctrlStatus.FaceId = 1087
Else
'disable Find next, clear addresses, update status
c.Activate
ctrlFN.Enabled = False
firstAddress = ""
lastaddress = ""
ctrlStatus.Caption = """" & ctrlText.Text & """" & " - no more instances found"
ctrlText.Text = ""
ctrlStatus.FaceId = 1088
End If
Set c = Nothing
End Sub
Sub ToggleFindWhole()
If iFindWhole = 1 Then
iFindWhole = 2
CommandBars.ActionControl.FaceId = 0
Else
iFindWhole = 1
CommandBars.ActionControl.FaceId = 990
End If
End Sub
'standard module code - END
'##################
'##################
'Workbook events
Private Sub Workbook_Open()
BuildToolBar
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
DeleteToolbar
End Sub
'Workbook events - END
'##################
|