18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Part VI<br />

Programming Excel with VBA<br />

FIGURE 41.5<br />

A message box with a longer message and a title.<br />

Creating UserForms: An Overview<br />

The InputBox and MsgBox functions do just fine for many cases, but if you need to obtain more information,<br />

you need to create a UserForm.<br />

Following is a list of the general steps that you typically take to create a UserForm:<br />

1. Determine exactly how the dialog box is going to be used and where it is to fit into your<br />

VBA macro.<br />

2. Activate the VB Editor and insert a new UserForm.<br />

3. Add the appropriate controls to the UserForm.<br />

4. Create a VBA macro to display the UserForm. This macro goes in a normal VBA module.<br />

5. Create event handler VBA procedures that are executed when the user manipulates the controls<br />

(for example, clicks the OK button). These procedures go in the code module for the<br />

UserForm.<br />

The following sections provide more details on creating a UserForm.<br />

Working with UserForms<br />

To create a dialog box, you must first insert a new UserForm in the VB Editor window. To activate the VB<br />

Editor, choose Developer ➪ Visual Basic (or press Alt+F11). Make sure that the correct workbook is<br />

selected in the Project window and then choose Insert ➪ UserForm. The VB Editor displays an empty<br />

UserForm, shown in Figure 41.6. When you activate a UserForm, the VB editor displays the Toolbox,<br />

which is used to add controls to the UserForm.<br />

720

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!