Part VI: Programming Excel with VBA
866
- Double-click the button.
The VB Editor window is activated, and an empty Sub procedure is created.
- Enter the following VBA statement before the End Sub statement:
MsgBox “Hello. You clicked the command button.” - Press Alt+F11 to return to Excel.
- (Optional) Adjust any other properties for the CommandButton, using the
Properties window. Choose Developer ➪ Controls ➪ Properties if the Properties
window isn’t visible. - Click the Design Mode button in the Developer ➪ Controls section to exit
design mode.
After performing the preceding steps, click the CommandButton to display the message box.
Note
You must enter the VBA code manually. You can’t create macros for controls using the VBA macro recorder.
However, you can record a macro and then execute it from an event procedure. For example, if you record a
macro named FormatCells, insert Call FormatCells as a VBA statement. Or, you can copy the recorded
code and paste it to your event procedure. n
Reviewing the Available ActiveX Controls
The sections that follow describe the ActiveX controls that are available for use in your worksheets.
On the CD
The companion CD-ROM contains a file that includes examples of all the ActiveX controls. This file is named
worksheet controls.xlsm.
CheckBox
A CheckBox control is useful for getting a binary choice: YES or NO, TRUE or FALSE, ON or OFF,
and so on.
The following is a description of the most useful properties of a CheckBox control:
l Accelerator: A letter that enables the user to change the value of the control by using
the keyboard. For example, if the accelerator is A, pressing Alt+A changes the value of the
CheckBox control. The accelerator letter is underlined in the Caption of the control.