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

Part VI: Programming Excel with VBA

4.Select the macro’s name and click Add to add the item to the list on the right.

5.(Optional) To change the icon, click Modify and choose a new image. You can also change the Display Name.

6.Click OK to close the Excel Options dialog box. The icon appears on your Quick Access toolbar.

More on Creating UserForms

Creating UserForms can make your macros much more versatile. You can create custom commands that display dialog boxes that look exactly like those that Excel uses. This section contains some additional information to help you develop custom dialog boxes that work like those that are built in to Excel.

Adding accelerator keys

Custom dialog boxes should not discriminate against those who prefer to use the keyboard rather than a mouse. All Excel dialog boxes work equally well with a mouse and a keyboard because each control has an associated accelerator key. The user can press Alt plus the accelerator key to work with a specific dialog box control.

Adding accelerator keys to your UserForms is a good idea. You do this in the Properties window by entering a character for the Accelerator property.

The letter that you enter as the accelerator key must be a letter that is contained in the caption of the object. However, it can be any letter in the text — not necessarily the first letter). You should make sure that an accelerator key is not duplicated in a UserForm. If you have duplicate accelerator keys, the accelerator key acts on the first control in the tab order of the UserForm. Then, pressing the accelerator key again takes you to the next control.

Some controls (such as edit boxes) don’t have a caption property. You can assign an accelerator key to a label that describes the control. Pressing the accelerator key then activates the next control in the tab order (which you should ensure is the edit box).

Controlling tab order

The previous section refers to a UserForm’s tab order. When you’re working with a UserForm, pressing Tab and Shift+Tab cycles through the dialog box’s controls. When you create a UserForm,

856

Chapter 41: Creating UserForms

you should make sure that the tab order is correct. Usually, it means that tabbing should move through the controls in a logical sequence.

To view or change the tab order in a UserForm, choose View Tab Order to display the Tab Order dialog box. You can then select a control from the list; use the Move Up and Move Down buttons to change the tab order for the selected control.

Learning More

Mastering UserForms takes practice. You should closely examine the dialog boxes that Excel uses to get a feeling for how dialog boxes are designed. You can duplicate many of the dialog boxes that Excel uses.

The best way to learn more about creating dialog boxes is by using the VBA Help system. Pressing F1 is the quickest way to display the Help window.

857

CHAPTER

Using UserForm

Controls in a

Worksheet

Chapter 41 presents an introduction to UserForms. If you like the idea of using dialog box controls — but don’t like the idea of creating a custom dialog box — this chapter is for you. It explains how to

enhance your worksheet with a variety of interactive controls, such as buttons, list boxes, and option buttons.

Why Use Controls on a

Worksheet?

The main reason to use controls on a worksheet is to make it easier for the user to provide input. For example, if you create a model that uses one or more input cells, you can create controls to allow the user to select values for the input cells.

Adding controls to a worksheet requires much less effort than creating a dialog box. In addition, you may not have to create any macros because you can link a control to a worksheet cell. For example, if you insert a CheckBox control on a worksheet, you can link it to a particular cell. When the CheckBox is checked, the linked cell displays TRUE. When the CheckBox is not checked, the linked cell displays FALSE.

Figure 42.1 shows an example that uses three types of controls: a

Checkbox, two sets of OptionButtons, and a ScrollBar.

IN THIS CHAPTER

Why use controls on a worksheet?

Using controls

The Controls Toolbox controls

859

Part VI: Programming Excel with VBA

On the CD

This workbook is available on the companion CD-ROM. The file is named mortgage loan.xlsm.

FIGURE 42.1

This worksheet uses UserForm controls.

Adding controls to a worksheet can be a bit confusing because Excel offers two different sets of controls, both of which you access by choosing Developer Controls Insert.

Form controls: These controls are unique to Excel.

ActiveX controls: These controls are a subset of those that are available for use on UserForms.

Figure 42.2 shows the controls that appear when you choose Developer Controls Insert. When you move your mouse pointer over a control, Excel displays a ToolTip that identifies the controls.

To add to the confusion, many controls are available from both sources. For example, a control named ListBox is listed in both Forms controls and ActiveX controls. However, they are two entirely different controls. In general, Forms controls are easier to use, but ActiveX controls provide more flexibility.

Note

This chapter focuses exclusively on ActiveX controls. n

A description of ActiveX controls appears in Table 42.1.

860

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