Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Excel_2010_Bible.pdf
Скачиваний:
26
Добавлен:
13.03.2015
Размер:
11.18 Mб
Скачать

Part VI: Programming Excel with VBA

Handling events

When you insert a UserForm, that form can also hold VBA Sub procedures to handle the events that are generated by the UserForm. An event is something that occurs when the user manipulates a control. For example, clicking a button causes an event. Selecting an item in a list box control also triggers an event. To make a UserForm useful, you must write VBA code to do something when an event occurs.

Event-handler procedures have names that combine the control with the event. The general form is the control’s name, followed by an underscore, and then the event name. For example, the procedure that is executed when the user clicks a button named MyButton is MyButton_Click.

Displaying a UserForm

You also need to write a procedure to display the UserForm. You use the Show method of the UserForm object. The following procedure displays the UserForm named UserForm1:

Sub ShowDialog()

UserForm1.Show

End Sub

This procedure should be stored in a regular VBA module (not the code module for the UserForm). If your VB project doesn’t have a regular VBA module, choose Insert Module to add one.

When the ShowDialog procedure is executed, the UserForm is displayed. What happens next depends upon the event-handler procedures that you create.

A UserForm Example

The preceding section is, admittedly, rudimentary. This section demonstrates, in detail, how to develop a UserForm. This example is rather simple. The UserForm displays a message to the

user — something that can be accomplished more easily by using the MsgBox function. However, a UserForm gives you more flexibility in terms of formatting and layout of the message.

On the CD

This workbook is available on the companion CD-ROM. The file is named show message.xlsm.

846

Chapter 41: Creating UserForms

Creating the UserForm

If you’re following along on your computer, start with a new workbook. Then follow these steps:

1.Choose Developer Visual Basic (or press Alt+F11) to activate the VB Editor window.

2.In the VB Editor Project window, double-click your workbook’s name to activate it.

3.Choose Insert UserForm. The VB Editor adds an empty form named UserForm1 and displays the Toolbox.

4.Press F4 to display the Properties window and then change the following properties of the UserForm object:

Property

Change To

Name

AboutBox

Caption

About This Workbook

5.Use the Toolbox to add a Label object to the UserForm.

6.Select the Label object. In the Properties window, enter any text that you want for the label’s Caption.

7.In the Properties window, click the Font property and adjust the font. You can change the typeface, size, and so on. The changes then appear in the form. Figure 41.8 shows an example of a formatted Label control. In this example, the TextAlign property was set to the code that center aligns the text.

2 - fmTextAlignCenter

FIGURE 41.8

A Label control, after changing its Font properties.

847

Part VI: Programming Excel with VBA

8.Use the Toolbox and add a CommandButton object to the UserForm, and use the Properties window to change the following properties for the CommandButton:

Property

Change To

Name OKButton

Caption OK

Default True

9.Make other adjustments so that the form looks good to you. You can change the size of the form or move or resize the controls.

Testing the UserForm

At this point, the UserForm has all the necessary controls. What’s missing is a way to display the UserForm. While you’re developing the UserForm, you can press F5 to display it and see how it looks. To close the UserForm, click the X button in the title bar.

This section explains how to write a VBA Sub procedure to display the UserForm when Excel is active.

1.Insert a VBA module by choosing Insert Module.

2.In the empty module, enter the following code:

Sub ShowAboutBox() AboutBox.Show

End Sub

3.Activate Excel. (Pressing Alt+F11 is one way.)

4.Choose Developer Code Macros to display the Macros dialog box. Or you can press Alt+F8.

5.In the Macros dialog box, select ShowAboutBox from the list of macros and then click Run. The UserForm then appears.

If you click the OK button, notice that it doesn’t close the UserForm as you may expect. This button needs to have an event-handler procedure in order for it to do anything when it’s clicked. To dismiss the UserForm, click the Close button (X) in its title bar.

Cross-Reference

You may prefer to display the UserForm by clicking a CommandButton on your worksheet. See Chapter 42 for details on attaching a macro to a worksheet CommandButton.

848

Chapter 41: Creating UserForms

Creating an event-handler procedure

An event-handler procedure is executed when an event occurs. In this case, you need a procedure to handle the Click event that’s generated when the user clicks the OK button.

1.Activate the VB Editor. (Pressing Alt+F11 is the fastest way.)

2.Activate the AboutBox UserForm by double-clicking its name in the Project window.

3.Double-click the CommandButton control. The VB Editor activates the code module for the UserForm and inserts some boilerplate code, as shown in Figure 41.9.

FIGURE 41.9

The code module for the UserForm.

4.Insert the following statement before the End Sub statement:

Unload AboutBox

This statement simply dismisses the UserForm by using the Unload statement. The complete event-handler procedure is

Private Sub OKButton_Click()

Unload AboutBox

End Sub

849

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]