Option Explicit
Sub FloatingBar()
Dim objBar As CommandBar
Dim objCombo As CommandBarComboBox
Dim objButton As CommandBarButton
Dim wks As Worksheet
Dim nm As Name
Dim strSheet() As String
Dim strRange() As String
Dim lngLeft As Long
Dim lngTop As Long
Dim lngPosition As Long
Dim i As Integer
On Error GoTo ErrHandler
i = 1
On Error Resume Next
Debug.Print ActiveWorkbook.Sheets.Count
If Err.Number <> 0 Then
MsgBox "There are no sheets in the current workbook.", vbInformation, "No Sheets Available"
Else
On Error GoTo ErrHandler
For Each wks In ActiveWorkbook.Sheets
ReDim Preserve strSheet(1 To i)
strSheet(i) = wks.Name
i = i + 1
Next wks
BubbleSort strSheet
On Error Resume Next
Debug.Print ActiveWorkbook.Names(1).Name
If Err.Number = 0 Then
On Error GoTo ErrHandler
i = 1
For Each nm In ActiveWorkbook.Names
If InStr(1, nm.Name, "Print_Area") = 0 And _
InStr(1, nm.Name, "Print_Titles") = 0 And _
InStr(1, nm.Name, "_FilterDatabase") = 0 Then
ReDim Preserve strRange(1 To i)
strRange(i) = nm.Name
i = i + 1
End If
Next nm
End If
On Error Resume Next
Set objBar = CommandBars.Add(Name:="Navigation", Position:=msoBarFloating, _
Temporary:=False)
If Err.Number <> 0 Then
Set objBar = CommandBars("Navigation")
lngLeft = objBar.Left
lngTop = objBar.Top
lngPosition = objBar.Position
objBar.Delete
Set objBar = CommandBars.Add(Name:="Navigation", Position:=msoBarFloating, _
Temporary:=False)
objBar.Left = lngLeft
objBar.Top = lngTop
objBar.Position = lngPosition
End If
On Error GoTo ErrHandler
objBar.Visible = True
Set objCombo = objBar.Controls.Add(Type:=msoControlComboBox)
With objCombo
.AddItem "<Select sheet name>"
For i = 1 To UBound(strSheet)
.AddItem strSheet(i)
Next i
.Width = 250
.Style = msoComboNormal
.OnAction = "GoToSheet"
.Caption = "Select Sheet"
.Text = "<Select sheet name>"
End With
On Error Resume Next
Debug.Print ActiveWorkbook.Names(1).Name
If Err.Number = 0 Then
Set objCombo = objBar.Controls.Add(Type:=msoControlComboBox)
With objCombo
.AddItem "<Select range name>"
For i = 1 To UBound(strRange)
.AddItem strRange(i)
Next i
.Width = 250
.Style = msoComboNormal
.OnAction = "GoToRange"
.Caption = "Select Range"
.Text = "<Select range name>"
End With
End If
Set objButton = objBar.Controls.Add(Type:=msoControlButton)
With objButton
.Caption = "Refresh List"
.Style = msoButtonCaption
.OnAction = "FloatingBar"
End With
End If
ExitHere:
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description
Resume ExitHere
End Sub
Sub GoToSheet()
Dim lngSelected As Long
Dim strSheet As String
On Error GoTo ErrHandler
lngSelected = CommandBars("Navigation").Controls(1).ListIndex
strSheet = CommandBars("Navigation").Controls(1).List(lngSelected)
If strSheet <> "<Select sheet name>" Then
On Error Resume Next
Debug.Print ActiveWorkbook.Sheets(strSheet).Visible
If Err.Number = 0 Then
If ActiveWorkbook.Sheets(strSheet).Visible = False Then
If vbYes = MsgBox("This sheet is is hidden." & vbCrLf & vbCrLf & _
"Do you want to unhide the sheet?", vbQuestion + vbYesNo + _
vbDefaultButton2, "Hidden Sheet") Then
ActiveWorkbook.Sheets(strSheet).Visible = True
ActiveWorkbook.Sheets(strSheet).Select
End If
Else
ActiveWorkbook.Sheets(strSheet).Select
End If
Else
MsgBox "That sheet could not be located in the current workbook." & _
vbCrLf & vbCrLf & "Please click on 'Refresh List' to update the " & _
"list of sheet names and ranges.", vbInformation, "Sheet Not Found"
End If
End If
ExitHere:
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description
Resume ExitHere
End Sub
Sub GoToRange()
Dim lngSelected As Long
Dim strRange As String
Dim strSheet As String
On Error GoTo ErrHandler
lngSelected = CommandBars("Navigation").Controls(2).ListIndex
strRange = CommandBars("Navigation").Controls(2).List(lngSelected)
If strRange <> "<Select range name>" Then
On Error Resume Next
Debug.Print ActiveWorkbook.Names(strRange).RefersTo
If Err.Number = 0 Then
strRange = ActiveWorkbook.Names(strRange).RefersTo
If InStr(1, LCase(strRange), ".xls") > 0 Then
MsgBox "This range refers to an external workbook.", vbInformation, _
"Cannot Select Range"
GoTo ExitHere
ElseIf InStr(1, strRange, "!") > 0 Then
strSheet = Left(strRange, InStr(1, strRange, "!") - 1)
strSheet = Replace(strSheet, "'", "")
strSheet = Replace(strSheet, "=", "")
strRange = Mid(strRange, InStr(1, strRange, "!") + 1)
If ActiveWorkbook.Sheets(strSheet).Visible = False Then
If vbYes = MsgBox("This range is on a hidden sheet." & vbCrLf & vbCrLf & _
"Do you want to unhide the sheet?", vbQuestion + vbYesNo + _
vbDefaultButton2, "Range on Hidden Sheet") Then
ActiveWorkbook.Sheets(strSheet).Visible = True
ActiveWorkbook.Sheets(strSheet).Select
Else
GoTo ExitHere
End If
Else
ActiveWorkbook.Sheets(strSheet).Select
End If
End If
Range(strRange).Select
Else
MsgBox "That range name could not be located in the current workbook." & _
vbCrLf & vbCrLf & "Please click on 'Refresh List' to update the " & _
"list of sheet names and ranges.", vbInformation, "Range Name Not Found"
End If
End If
ExitHere:
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description
Resume ExitHere
End Sub
Public Sub BubbleSort(ByRef strArray() As String)
Dim lngOuter As Long
Dim lngInner As Long
Dim lngLBound As Long
Dim lngUBound As Long
Dim strTemp As String
lngLBound = LBound(strArray)
lngUBound = UBound(strArray)
For lngOuter = lngLBound To lngUBound
For lngInner = lngLBound To lngUBound - lngOuter
If strArray(lngInner) > strArray(lngInner + 1) Then
strTemp = strArray(lngInner)
strArray(lngInner) = strArray(lngInner + 1)
strArray(lngInner + 1) = strTemp
End If
Next lngInner
Next lngOuter
End Sub
|