18.11.2014 Views

Microsoft Office

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Part VI<br />

Programming Excel with VBA<br />

TABLE 44.2 (continued)2 (CONTINUED)<br />

Property<br />

Description<br />

Value<br />

Left and Top<br />

Width and Height<br />

Visible<br />

Name<br />

Picture<br />

The control’s value.<br />

Values that determine the control’s position.<br />

Values that determine the control’s width and height.<br />

If False, the control is hidden.<br />

The name of the control. By default, a control’s name is based on the control type. You can<br />

change the name to any valid name. However, each control’s name must be unique on the<br />

worksheet.<br />

Enables you to specify a graphic image to display.<br />

Linking controls to cells<br />

Often, you can use ActiveX controls in a worksheet without using any macros. Many controls have a<br />

LinkedCell property, which specifies a worksheet cell that is “linked” to the control.<br />

For example, you may add a SpinButton control and specify cell B1 as its LinkedCell property. After<br />

doing so, cell B1 contains the value of the SpinButton, and clicking the SpinButton changes the value<br />

in cell B1. You can, of course, use the value contained in the linked cell in your formulas.<br />

NOTE<br />

When specifying the LinkedCell property in the Properties window, you can’t “point” to<br />

the linked cell. You must type the cell address. Also, cell and range names aren’t allowed.<br />

Creating macros for controls<br />

To create a macro for a control, you must use the Visual Basic Editor (VB Editor). The macros are stored in<br />

the code module for the sheet that contains the control. For example, if you place a control on Sheet2, the<br />

VBA code for that control is stored in the Sheet2 code module. Each control can have a macro to handle any<br />

of its events. For example, a CommandButton control can have a macro for its Click event, its<br />

DblClick event, and various other events.<br />

TIP<br />

The easiest way to access the code module for a control is to double-click the control while in<br />

design mode. Excel displays the VB Editor and creates an empty procedure for the control’s<br />

default event. For example, the default event for a CommandButton control is the Click event. Figure 42.4<br />

shows the autogenerated code for a control named CheckBox1, located on Sheet1.<br />

The control’s name appears in the upper-left portion of the code window, and the event appears in the<br />

upper-right area. If you want to create a macro that executes when a different event occurs, select the event<br />

from the list in the upper-right area.<br />

The following steps demonstrate how to insert a CommandButton and create a simple macro that displays<br />

a message when the button is clicked:<br />

1. Choose Developer ➪ Controls and click the Insert drop-down control.<br />

2. Click the CommandButton tool in the ActiveX Controls section.<br />

3. Click and drag in the worksheet to create the button. Excel automatically enters design mode.<br />

738

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

Saved successfully!

Ooh no, something went wrong!