|
|
|
|
|
|
Excel
|
Alphabetized Table of Contents with Toolbar Buttons
|
|
Ease of Use
|
Easy
|
Version tested with
|
2000
|
Submitted by:
|
XLGibbs
|
Description:
|
This code inserted into a project or workbook would allow the ongoing creation of a custom toolbar which acts as a table of contents for the workbook it is created from. Since it is workbook specific, the code provided allows for deletion of the toolbar when the workbook is closed or deactivated. Once the code is inserted into a workbook, it is self sustaining within that workbook.
|
Discussion:
|
Sometimes there are workbooks with so many sheets and/or charts, or the names are too long, that navigating to those sheets using the tabs is cumbersome or inconvenient. This toolbar "on the fly" allows for quick toggling between the list of sheets by having a button for each sheet or chart on separate toolbar menus. Application of this code is fairly easy, however, since it is designed to be contained in only one workbook at a time. This code can appear and work in multiple files open at the same time, but it is imperative that the Workbook level code be applied to ensure that the toolbar created is only for the active workbook.
|
Code:
|
instructions for use
|
Option Explicit
Sub CreateSheetList()
Dim wb As Workbook, cBar As CommandBar, cBarItem As CommandBarButton, cBarButton As CommandBarPopup
Dim sheetnames() As String, i, SheetCount As Integer, chartnames() As String, chartcount As Integer
Toggle
Call DeleteSheetList
Set wb = ActiveWorkbook
With wb
SheetCount = ActiveWorkbook.Sheets.Count
Set cBar = Application.CommandBars.Add("Sheet List", Position:=msoBarTop, _
Temporary:=True)
Set cBar = Application.CommandBars("Sheet List")
With cBar
Set cBarButton = cBar.Controls.Add(Type:=msoControlPopup)
cBarButton.Caption = "GoTo Sheet"
ReDim sheetnames(1 To SheetCount)
For i = 1 To UBound(sheetnames)
sheetnames(i) = Sheets(i).Name
Next i
Call BubbleSort(sheetnames)
For i = 1 To SheetCount
If Sheets(sheetnames(i)).Visible = True Then
Set cBarItem = cBarButton.Controls.Add(Type:=msoControlButton)
With cBarItem
.Caption = sheetnames(i)
.OnAction = "'SheetCall """ & .Caption & """'"
.FaceId = 142
.BeginGroup = True
.Style = msoButtonIconAndCaption
End With
End If
Next i
chartcount = wb.Charts.Count
Debug.Print chartcount
If chartcount > 0 Then
Set cBarButton = cBar.Controls.Add(Type:=msoControlPopup)
cBarButton.Caption = "GoTo Chart"
ReDim charttnames(1 To chartcount)
For i = 1 To chartcount
sheetnames(i) = Charts(i).Name
Next i
If chartcount > 1 Then Call BubbleSort(sheetnames)
For i = 1 To chartcount
If Charts(sheetnames(i)).Visible = True Then
Set cBarItem = cBarButton.Controls.Add(Type:=msoControlButton)
With cBarItem
.Caption = sheetnames(i)
.OnAction = "'ChartCall """ & .Caption & """'"
.FaceId = 142
.BeginGroup = True
.Style = msoButtonIconAndCaption
End With
End If
Next i
End If
End With
End With
cBar.Visible = True
Toggle
Set cBar = Nothing: Set cBarItem = Nothing: Set cBarButton = Nothing: Set wb = Nothing
End Sub
Sub BubbleSort(sheetnames() As String)
Dim First As Integer, Last As Integer
Dim i As Integer, j As Integer, Temp As String
First = LBound(sheetnames): Last = UBound(sheetnames)
For i = First To Last - 1
For j = i + 1 To Last
If UCase(sheetnames(i)) > UCase(sheetnames(j)) Then
Temp = sheetnames(j)
sheetnames(j) = sheetnames(i)
sheetnames(i) = Temp
End If
Next j
Next i
End Sub
Sub Toggle()
With Application
.ScreenUpdating = Not .ScreenUpdating
.EnableEvents = Not .EnableEvents
End With
End Sub
Sub DeleteSheetList()
On Error Resume Next
Application.CommandBars("Sheet List").Delete
Application.CommandBars("Chart List").Delete
On Error GoTo 0
End Sub
Sub SheetCall(ByVal Sh As String)
On Error GoTo ErrHandler
Sheets(Sh).Activate
Exit Sub
ErrHandler:
CreateSheetList
End Sub
Sub ChartCall(ByVal Ch As String)
On Error GoTo ErrHandler
Charts(Ch).Activate
Exit Sub
ErrHandler:
CreateSheetList
End Sub
Option Explicit
Private Sub Workbook_Activate()
CreateSheetList
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
DeleteSheetList
End Sub
Private Sub Workbook_Deactivate()
DeleteSheetList
End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object)
CreateSheetList
End Sub
Private Sub Workbook_Open()
CreateSheetList
End Sub
|
How to use:
|
- Open Excel, and Open the Visual Basic Editor (Press Alt-F11, or Tools>Macros>Visual Basic Editor)
- Right click the VBAProject for your workbook and Insert > Module
- Paste the top portion of the code (Designated with "Standard Module") into the code window.
- Double Click the "Workbook" module in your project. Paste the Workbook module code provided in the code window.
- Save your workbook, and run the code in the standard module "CreateSheetList" by pressing the green play button in the VBE window, or by pressing Alt-F8 in the workbook window.
- The toolbar will be created and you will see the title "GoTo Sheets" and/or "GoTo Chart". The sheets in your workbook can be activated by selecting them from this list.
|
Test the code:
|
- After placing the code in a workbook and saving, open up another workbook so there are two active in windows.
- Simply go back and forth between the two files (one with, one without the code) to see the toolbar appear and dissappear.
- Or you can insert all of the code as instructed, close and re-open your file.
- The attached sample is a working demonstration.
- Note: For code simplicity, adding/deleting/renaming a sheet is not captured, however, the changes will be reflected on the toolbar if the workbook is closed and reopened, or deactivated and reactivated.
|
Sample File:
|
SheetListToolbar_V2.1.zip 146.69KB
|
Approved by mdmackillop
|
This entry has been viewed 380 times.
|
|