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

More on Creating UserForms<br />

Creating UserForms can make your macros much more versatile. You can create custom commands that<br />

display dialog boxes that look exactly like those that Excel uses. This section contains some additional<br />

information to help you develop custom dialog boxes that work like those that are built into Excel.<br />

Adding accelerator keys<br />

Custom dialog boxes should not discriminate against those who prefer to use the keyboard rather than a<br />

mouse. All of Excel’s dialog boxes work equally well with a mouse and a keyboard because each control has<br />

an associated accelerator key. The user can press Alt plus the accelerator key to work with a specific dialog<br />

box control.<br />

Adding accelerator keys to your UserForms is a good idea. You do this in the Properties window by entering<br />

a character for the Accelerator property.<br />

The letter that you enter as the accelerator key must be a letter that is contained in the caption of the object.<br />

It can be any letter in the text (not necessarily the first letter). You should make sure that an accelerator key<br />

is not duplicated in a UserForm. If you have duplicate accelerator keys, the accelerator key acts on the first<br />

control in the tab order of the UserForm. Then, pressing the accelerator key again takes you to the next<br />

control.<br />

Some controls (such as edit boxes) don’t have a caption property. You can assign an accelerator key to a<br />

label that describes the control. Pressing the accelerator key then activates the next control in the tab order<br />

(which you should ensure is the edit box).<br />

Controlling tab order<br />

The previous section refers to a UserForm’s tab order. When you’re working with a UserForm, pressing Tab<br />

and Shift+Tab cycles through the dialog box’s controls. When you create a UserForm, you should make sure<br />

that the tab order is correct. Usually, it means that tabbing should move through the controls in a logical<br />

sequence.<br />

To view or change the tab order in a UserForm, choose View ➪ Tab Order to display the Tab Order dialog<br />

box. You can then select a control from the list; use the Move Up and Move Down buttons to change the tab<br />

order for the selected control.<br />

Learning More<br />

Mastering UserForms takes practice. You should closely examine the dialog boxes that Excel uses to get a<br />

feeling for how dialog boxes are designed. You can duplicate many of the dialog boxes that Excel uses.<br />

The best way to learn more about creating dialog boxes is by using the VBA Help system.<br />

732

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

Saved successfully!

Ooh no, something went wrong!