18.11.2014 Views

Microsoft Office

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Part VI<br />

Programming Excel with VBA<br />

FIGURE 41.10<br />

The UserForm after adding controls and adjusting some properties.<br />

TIP<br />

The VB Editor provides several useful commands to help you size and align the controls. Select<br />

the controls that you want to work with and then choose a command from the Format menu.<br />

These commands are fairly self-explanatory, and the Help system has complete details.<br />

Testing the UserForm<br />

At this point, the UserForm has all the necessary controls. What’s missing is a way to display the form. This<br />

section explains how to write a VBA procedure to display the UserForm.<br />

1. Make sure that the VB Editor window is activated.<br />

2. Insert a module by choosing Insert ➪ Module.<br />

3. In the empty module, enter the following code:<br />

Sub ShowUserForm()<br />

UserForm1.Show<br />

End Sub<br />

4. Choose Run ➪ Sub/UserForm (or press F5).<br />

The Excel window is then activated, and the new UserForm is displayed, as shown in Figure 41.11. The<br />

OptionButton controls work, but clicking the OK and Cancel buttons has no effect. These two buttons<br />

need to have event-handler procedures. Click the Close button in the title bar to dismiss the UserForm.<br />

728

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

Saved successfully!

Ooh no, something went wrong!