Using a List Box in a User Form

Figure 14-2 shows another example that uses a ListBox as a menu. Before the UserForm is displayed, its Initialize event handler procedure is called. This procedure, which follows, uses the Addltem method to add six items to the ListBox:

ListBox Menu Demo


1 Select a macro:

1 Macrol



Cancel |

1 Macros_1

_ Zl

1 J Execute |

Figure 14-2: This dialog box uses a ListBox as a menu.

Figure 14-2: This dialog box uses a ListBox as a menu.

Private Sub UserForm_Initialize() With ListBoxl

.Addltem "Macrol" .Addltem "Macro2"

End End Sub

.AddItem .AddItem .AddItem .AddItem With

"Macro3" "Macro4" "Macro5" "Macro6"

The Execute button also has a procedure to handle its Click event:

Private Sub ExecuteButton_Click() Select Case ListBoxl.ListIndex Case -1

MsgBox "Select a macro from the list."

Exit Sub Case 0: Call Macrol Case 1: Call Macro2 Case 2: Call Macro3 Case 3: Call Macro4 Case 4: Call Macro5 Case 5: Call Macro6 End Select Unload Me End Sub

This procedure accesses the Listlndex property of the ListBox to determine which item is selected. (If the Listlndex is -1, nothing is selected.) Then the appropriate macro is executed.

Excel,of course,also lets you create real menus and toolbars. Refer to Chapters 22 and 23 for details.

0 0

Post a comment