Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Creating the Navigation Userform

Press Alt+F11 to access the VBA editor.


From the menu choose Insert / Userform
With the module selected Right Click and choose Properties and name the
userform frmNavigation.
Change the property for the Userform ShowModal to False.
In Properties select Caption and add “Navigation for workbook”
Add a label and for the caption add the text “Double Click to go to Visible Sheets”.
This text will not be referred to in our code.
Add a list box and a command button with the names below.

 Change the  name ListBox1                           lstSheet


 Change the name CommandButton1        cmdClose2 and add a caption for this
button called“Close”

To make this all happen we need to add four macros (procedures) to the userform and
a small piece of code needs to be added to a module.

These macros will accomplish:


 Close the userform
 Reset the userform
 Add the sheets to the userform listbox
 Select the sheet when double clicked

Adding Userform Code


Double click in the worksheets userform to active the code area and paste the code
below into the userform.

Here is an explanation of what the code is doing.


The cmdClose2 and the cmdReset are self-explanatory.
We will focus first on the Private Sub UserForm_Initialize.
This event occurs when the userform is called. This code will populate the Userform
with the list of worksheets in the application.
It loops through the sheets and added the sheet names as list items in the listbox.
For Each Sh In ActiveWorkbook.Sheets
Me.lstSheet.AddItem Sh.Name
Next Sh
I have added an If statement that will collect only visible sheets and exclude the Login
sheet.
If Sh.Visible = True And Sh.Name <> "Login" Then
The other event we will be using is the Private Sub lstSheet_DblClick this event
occurs as the name indicates when the listbox item is double clicked.
For I = 0 To lstSheet.ListCount- 1
If lstSheet.Selected(I) = True Then
Sht = lstSheet.List(I)
End If
Next I
Sheets(Sht).Select
A If statement is added to check if the sheet is already active.
If ActiveSheet.Name = Sht Then
MsgBox "This sheet is already open!"
Exit Sub
End If

 Here is the code to add.


Option Explicit
Private Sub cmdClose2_Click()
'unload the userform
Unload Me
End Sub
Private Sub cmdReset_Click()
'reset the form
Unload Me
frmNavigation.Show
End Sub
Private Sub lstSheet_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'declare the variables
Application.ScreenUpdating = False
Dim I As Integer, Sht As String
'for loop
For I = 0 To lstSheet.ListCount- 1
'get the name of the selected sheet
If lstSheet.Selected(I) = True Then
Sht = lstSheet.List(I)
End If
Next I
'test if sheet is already open
If ActiveSheet.Name = Sht Then
MsgBox "This sheet is already open!"
Exit Sub
End If
'select the sheet
Sheets(Sht).Select
'reset the userform
Unload Me
frmNavigation.Show
End Sub
Private Sub UserForm_Initialize()
'declare the variable
Dim Sh As Variant
'for each loop the add visible sheets
For Each Sh In ActiveWorkbook.Sheets
'only visible sheetand exclude login sheet
If Sh.Visible = True And Sh.Name <> "Login" Then
'add sheets to the listbox
Me.lstSheet.AddItem Sh.Name
End If
Next Sh
End Sub
 

This code goes into a module


(if you added this code in a previous tutorial then ignore this)
Now we need to insert a module to hold a little bit of code.
Press Alt+F11 to access the VBA editor.
From the menu choose Insert / Module.
Sub ShowNav()
frmNavigation.Show
End Sub
 

Assign the userform to a button on the


worksheets
If you would like the users to be able to access this userform add a shape to each work
sheet with the text “Navigate” and then Right Click the shape and select Assign
Macro. Choose the name ShowNav
 
 

You might also like