Excel

Page 292

Chapter 18

UserForm Techniques and Tricks In This Chapter Using a custom dialog box in your application Creating a dialog box: A hands-on example

T

he previous chapters show you how to insert a UserForm (which contains a custom dialog box), add controls to the UserForm, and adjust some of the control’s properties. These skills, however, won’t do you much good unless you understand how to make use of UserForms in your VBA code. This chapter provides these missing details and presents some useful techniques and tricks in the process.

Using Dialog Boxes When you use a custom dialog box in your application, you normally write VBA code that does the following: Initializes the UserForm controls. For example, you may write code that sets the default values for the controls. Displays the dialog box by using the UserForm object’s Show method. Responds to the events of the various controls. Validates the information provided by the user (if the user did not cancel the dialog box). This step is optional. Takes some action with the information provided by the user (if the information is valid).

A UserForm Example This example demonstrates the five points I describe in the preceding section. You use a dialog box to get two pieces of information: a person’s name and sex. The dialog box uses a TextBox control to get the name and three


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.