Example The Activate SelectedSheet Procedure

Sub ActivateSelectedSheet()

If lstSheets.ListIndex > -1 Then

Sheets(lstSheets.List(lstSheets.ListIndex)).Activate End If Unload Me End Sub

This code demonstrates some list box properties. First, the ListIndex property returns the index number (starting at 0) of the currently selected item in the list box. Thus, the following code checks to see if an item is selected (otherwise ListIndex = -1):

If lstSheets.ListIndex > -1 Then The code:

lstSheets.List(i)

returns the ith item in the list box (as a string). Thus:

lstSheets.List(lstSheets.ListIndex))

is the currently selected item—that is, the currently selected sheet name. Finally, the code:

Sheets(lstSheets.List(lstSheets.ListIndex)).Activate activates that worksheet by invoking its Activate method. We will discuss the Activate method in Chapter 18. For now, we simply note that if a worksheet has the name MySheet, then the code:

Sheets("MySheet").Activate activates that sheet.

Finally, the last thing done in the cmdActivate_Click event is to unload the form, since it is no longer needed.

0 0

Post a comment