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 />

Procedures in Module1<br />

The two macro that follow are contained in the Module1 code module. The ShowUserForm procedure<br />

checks the type of selection. If a range is selected, the dialog box in UserForm1 appears. If anything other<br />

than a range is selected, a message box is displayed.<br />

The ChangeCaseOfText procedure is a special callback procedure, with one argument, that is executed<br />

from a control on the Ribbon. See “Creating the user interface for your add-in macro,” later in this chapter.<br />

This procedure simply executes the ShowUserForm procedure.<br />

Sub ShowUserForm()<br />

If TypeName(Selection) = “Range” Then<br />

UserForm1.Show<br />

Else<br />

MsgBox “Select some cells.”<br />

End If<br />

End Sub<br />

Sub ChangeCaseOfText(ByVal control As IRibbonControl)<br />

Call ShowUserForm<br />

End Sub<br />

About the UserForm<br />

Figure 45.3 shows the UserForm1 form, which has five controls: three OptionButton controls and two<br />

CommandButton controls. The controls have descriptive names, and the Accelerator property is set so<br />

that the controls display an accelerator key (for keyboard users). The option button with the Upper Case<br />

caption has its Value property set to True, which makes it the default option.<br />

The UserForm1 object contains the event-handler procedures for the two CommandButton objects that are<br />

on the form. The following procedure is executed when the OK button is clicked. This procedure does all<br />

the work:<br />

FIGURE 45.3<br />

The custom dialog box.<br />

776

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

Saved successfully!

Ooh no, something went wrong!