Datasheet

24
1
4
2
3
The Record Macro dialog
box appears.
4 Click OK.
5 Record your macro.
Note: See section “Record a
Macro” to learn how to record
a macro.
1 Right-click your control
twice.
A menu appears.
2 Click Assign Macro.
The Assign Macro dialog
box appears.
Excel assigns a default
macro name for the
selected control.
3 Click Record.
Add a Macro to a Form Control
Y
ou can use macros to automate the tasks you
perform in Excel. You can assign a macro to any
form control on a worksheet. For example, if a
user clicks a button control, you can have Excel execute a
macro.
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 Visual Basic for Applications
(VBA) procedure. When you select the Assign Macro
menu option, Excel automatically creates a new macro
name by using 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
a worksheet. For example, the first OptionButton control
that 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. Every time you
add a new control, Excel gives the control a unique name
by adding a sequential number to OptionButton; for
example, OptionButton2_Click, Option Button3_
Click
, and so on.
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, 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.
Add a Macro to
a Form Control
03_591598-ch01.indd 2403_591598-ch01.indd 24 6/11/10 1:41 PM6/11/10 1:41 PM