Datasheet
Add a Macro to
a Form Control
Y
ou can assign a macro to any form control on a
worksheet. For example, if a user clicks a radio
button control, you can have Excel add a postage
amount to an invoice.
You can create one macro for each control on a
worksheet. You create a macro either by recording a
series of keystrokes or by writing a VBA procedure in the
Visual Basic Editor. When you select the Assign Macro
menu option, Excel automatically creates a new macro
with the name of the control followed by an underscore
and an event name, such as _Click. Excel assigns the
control name to the control when you add it to your
worksheet. For example, the first OptionButton control
you add to a worksheet is named OptionButton1. If you
create a macro for the option button, Excel gives the
macro the name OptionButton1_Click.
The portion of the macro name following the underscore
character corresponds to an action, commonly referred to
as an event. For example, with an OptionButton control,
the user clicks the radio button to select the option, and
so the event is Click. If you create a macro for a combo
box control, Excel assigns Change to the name of the
event because you want to execute the macro when the
value of the control changes. The event extension tells
Excel to monitor the control and execute the macro
whenever a user clicks the control.
No matter which option you select — recording or writing
VBA — Excel assigns the same name to the macro.
1 Right-click your control.
A menu appears.
2 Click Assign Macro.
1
2
3
Add a Macro to a Form Control
24
The Assign Macro dialog box appears.
Excel assigns a default macro name for the selected
control.
3 Click Record and then record your macro.
Note:
See the section, “Record a Macro,” for more
information.
04_132302 ch01.qxp 7/27/07 9:20 PM Page 24