Example: The ActivateSheet Utility

It is time now to implement the ActivateSheet utility in our SRXUtils application. This will demonstrate the use of UserForms.

In particular, when the user selects ActivateSheet, we would like to present her with a custom dialog that lists all of the sheets in the active workbook, as shown in Figure 14-5. The user can select one of these sheets, which will then be activated.

The activate sheet dialog

Figure 14-5. The activate sheet dialog

To implement this utility, we need to do the following:

So crank up the SRXUtils.xls worksheet and replace the ActivateSheet procedure in basMain:

Public Sub ActivateSheet()
  MsgBox "This is the ActivateSheet utility"
End Sub

with the procedure:

Public Sub ActivateSheet()
  dlgActivateSheet.Show
End Sub

which simply displays the Activate Sheet dialog (which we will call dlgActivateSheet).

After you insert a UserForm into your project, you should use the Properties window to change its Name property to dlgActivateSheet and its Caption property to "Activate Sheet." Then you can add the controls to the form. The UserForm in Figure 14-5 has two command buttons and one list box.

Place a List box on the form as in Figure 14-5. Using the Properties window, set the properties shown in Table 14-1. Note that the TabIndex property determines not only the order that the controls are visited as the user hits the Tab key, but also determines which control has the initial focus. Since we want the initial focus to be on the list box, we set its tab index to 0.

We should also note that, in general, there are two places in which a control property can be set: in the Properties window at design time or using code during run time. Some properties should be (or must be) set at design time, whereas others can only be set at run time. However, most properties can be set at either time.

As a simple example, a control's Visible or Enabled property is often set during run time, in response to actions by the user. For instance, we may want to disable a command button labeled Print until the user has selected an object to print from a list of objects. Setting the Enabled property of a command button whose name is PrintButton is easily done:

PrintButton.Enabled = False

In general, the choice of where to set a given property of a control is partly a matter of taste. I favor setting properties in code because it tends to make the code more complete and thus more readable. It can also make changing properties simpler. However, some fundamental properties, such as Name and Caption, are best set at design time.

Now it is time to create the code behind these controls.

Next, we create a procedure that will activate the selected sheet. We want this procedure to be called in three situations; namely, when the user:

Since this code will be used in three different situations, we can avoid repeating the code by placing it in its own procedure in the General section of a UserForm, as shown in Example 14-1.

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.

If all has gone well, you can now save SRXUtils as an add-in, load it through the Tools menu (if it is currently loaded, you will need to unload it before saving the add-in or Excel will complain), and try out the new ActivateSheet feature.