Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Excel_2010_Bible.pdf
Скачиваний:
26
Добавлен:
13.03.2015
Размер:
11.18 Mб
Скачать

Part VI: Programming Excel with VBA

The Sub procedure that follows is another example of using the MsgBox function:

Sub GetAnswer2()

Msg = “Do you want to process the monthly report?” Msg = Msg & vbNewLine & vbNewLine

Msg = Msg & “Processing the monthly report will take approximately“ Msg = Msg & “15 minutes. It will generate a 30-page report for all“ Msg = Msg & “sales offices for the current month.”

Title = “XYZ Marketing Company” Config = vbYesNo + vbQuestion Ans = MsgBox(Msg, Config, Title) If Ans = vbYes Then RunReport If Ans = vbNo Then Exit Sub

End Sub

This example demonstrates an efficient way to specify a longer message in a message box. A variable (Msg) and the concatenation operator (&) are used to build the message in a series of statements. vbNewLine is a constant that represents a break character. (Using two line breaks inserts a blank line.) The title argument is also used to display a different title in the message box. The Config variable stores the constants that generate Yes and No buttons and a question mark icon. Figure 41.5 shows how this message box appears when the procedure is executed.

FIGURE 41.5

A message box with a longer message and a title.

Creating UserForms: An Overview

The InputBox and MsgBox functions are adequate for many situations, but if you need to obtain more information, you need to create a UserForm.

The following is a list of the general steps that you typically take to create a UserForm:

1.Determine exactly how the dialog box is going to be used and where it is to fit into your VBA macro.

842

Chapter 41: Creating UserForms

2.Activate the VB Editor and insert a new UserForm.

3.Add the appropriate controls to the UserForm.

4.Create a VBA macro to display the UserForm. This macro goes in a normal VBA module.

5.Create event handler VBA procedures that are executed when the user manipulates the controls (for example, clicks the OK button). These procedures go in the code module for the UserForm.

The following sections provide more details on creating a UserForm.

Working with UserForms

To create a dialog box, you must first insert a new UserForm in the VB Editor window. To activate the VB Editor, choose Developer Visual Basic (or press Alt+F11). Make sure that the correct workbook is selected in the Project window and then choose Insert UserForm. The VB Editor displays an empty UserForm, shown in Figure 41.6. When you activate a UserForm, the VB editor displays the Toolbox, which is used to add controls to the UserForm.

FIGURE 41.6

An empty UserForm.

Adding controls

The Toolbox, also shown in Figure 41.6, contains various ActiveX controls that you can add to your UserForm.

843

Part VI: Programming Excel with VBA

When you move the mouse pointer over a control in the Toolbox, the control’s name appears. To add a control, click and drag it into the form. After adding a control, you can move it or change its size.

Table 41.2 lists the Toolbox controls.

TABLE 41.2

 

Toolbox Controls

Control

Description

 

 

Select Objects

Lets you select other controls by dragging.

 

 

Label

Adds a label (a container for text).

 

 

TextBox

Adds a text box (allows the user to type text).

 

 

ComboBox

Adds a combo box (a drop-down list).

 

 

ListBox

Adds a list box (to allow the user to select an item from a list).

 

 

CheckBox

Adds a check box (to control Boolean options).

 

 

OptionButton

Adds an option button (to allow a user to select from multiple options).

 

 

ToggleButton

Adds a toggle button (to control Boolean options).

 

 

Frame

Adds a frame (a container for other objects).

 

 

CommandButton

Adds a command button (a clickable button).

 

 

TabStrip

Adds a tab strip (a container for other objects).

 

 

MultiPage

Adds a multipage control (a container for other objects).

 

 

ScrollBar

Adds a scroll bar (to specify a value by dragging a bar).

 

 

SpinButton

Adds a spin button (to specify a value by clicking up or down).

 

 

Image

Adds a control that can contain an image.

 

 

RefEdit

Adds a reference edit control (lets the user select a range).

 

 

Cross-Reference

You can also place some of these controls directly on your worksheet. See Chapter 42 for details. n

Changing the properties of a control

Every control that you add to a UserForm has several properties that determine how the control looks and behaves. You can change some of these properties (such as Height and Width) by clicking and dragging the control’s border. To change other properties, use the Properties window.

844

Chapter 41: Creating UserForms

To display the Properties window, choose View Properties Window (or press F4). The Properties window displays a list of properties for the selected control. (Each control has a different set of properties.) If you click the UserForm itself, the Properties window displays properties for the form. Figure 41.7 shows the Properties window for a CommandButton control.

To change a property, select the property in the Properties window and then enter a new value. Some properties (such as BackColor) enable you to select a property from a list. The top of the Properties window contains a drop-down list that enables you to select a control to work with. You can also click a control to select it and display its properties.

FIGURE 41.7

The Properties window for a CommandButton control.

When you set properties via the Properties window, you’re setting properties at design time. You can also use VBA to change the properties of controls while the UserForm is displayed (that is, at run time).

A complete discussion of all the properties is well beyond the scope of this book — and it would indeed be very dull reading. To find out about a particular property, select it in the Properties window and press F1. The Help for UserForm controls is extremely thorough.

845

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]