Create A Custom Dialog

You can create custom dialog boxes that you can use with any of your macros. Dialog boxes add a graphical user interface, which enables the user to execute tasks such as clicking buttons to indicate a desired selection, or typing appropriate values. When you use VBA to create macros, you also gain most Visual Basic features, including the ability to create custom dialog boxes. VBA refers to these custom dialog boxes as Forms or UserForms.

You can change the name of a UserForm to make it easier to identify when you look at the UserForms listed in the Project window. To change the name of the UserForm, you need to change its Name property within the Properties window.

You create a custom dialog box within the Visual Basic Editor via the UserForm option. When you do this, the Visual Basic Editor creates a new UserForm called UserForm1 within the Forms folder on the Project window. See Chapter 2 for more information about the Projects window. Keep in mind, the Forms folder only displays if you have created UserForms for the current project.

After you create the UserForm, you can design it using the various Toolbox controls, which only display when you select the UserForm window. You add controls to the UserForm by dragging them from the Toolbox to the appropriate location on the UserForm. For example, if you want to request a text value from the user you drag the TextBox control onto the UserForm. After you add a control, you can resize it as needed. The Visual Basic Editor applies default values for each of the control properties. You can change the assigned values to the properties for the control within the Properties window. Keep in mind that you need to select the control on the UserForm before you can set the properties.

CREATE A CUSTOM DIALOG BOX

CREATE A CUSTOM DIALOG BOX

—n In the Projects window select the project where you want to add the new UserForm.

—0 Click Insert O UserForm.

■ The Visual Basic Editor creates a blank UserForm with a default name of UserForm1.

—n In the Projects window select the project where you want to add the new UserForm.

—0 Click Insert O UserForm.

■ The Visual Basic Editor creates a blank UserForm with a default name of UserForm1.

< Type a new name for the UserForm in the Name field of the Properties window.

—Q Click the UserForm.

0 0

Post a comment