Tải bản đầy đủ - 0trang
Example 10-1. The Workbook's Open and BeforeClose Event Handlers
' Set tag property to find it later for deletion
cbcpop.Tag = "SRXUtilsCustomMenu"
' Add menu item to popup menu
With cbcpop.Controls.Add(Type:=msoControlButton, _
.Caption = "&ActivateSheet"
.OnAction = "ActivateSheet"
Also place the DeleteCustomMenuItem procedure shown in Example 10-3 into basMenus:
Example 10-3. The DeleteCustomMenuItem Procedure
Dim cbc As CommandBarControl
Set cbc = Application.CommandBars( _
"Worksheet menu bar"). _
If Not cbc Is Nothing Then cbc.Delete
10.3.3 Implementing the Features of the Add-In
Since the ActivateSheet utility (which is invoked when the user selects the ActivateSheet
custom menu item created by the code in Example 10-2) is very simple, it does not require its own
code module. We simply add the following procedure to the basMain code module, which we
also must create:
Public Sub ActivateSheet()
MsgBox "This is the ActivateSheet utility"
10.3.4 Final Steps
Finally, you should follow these steps:
1. Compile the project. Use the Debug menu to compile the SRXUtils.xls project.
2. Set the properties. Set the workbook and project properties as shown in Figure 10-6 and
Figure 10-8, making any necessary changes based on the name you have chosen for your
3. Protect the add-in. Under the Protection tab of the project's Properties dialog, check the
"Lock project for viewing" checkbox and enter a password.
4. Save the add-in. Save the project as an add-in named SRXUtils.xla in a directory of your
Now we are ready to try the add-in. Close the SRXUtils.xls workbook and open a new workbook.
Select the Add-Ins menu item under the Tools menu and hit the Browse button on the Add-Ins
dialog. Locate your SRXUtils.xla file. Then check the entry in the Add-Ins dialog. You should see
the new Custom menu in the worksheet menu bar. Select the ActivateSheet item. You should get
the expected message box. Finis.
As mentioned earlier, as we progress through the book, we will make this example add-in much
Chapter 11. Excel Events
During the course of using Excel, certain events happen. For instance, when a worksheet is created,
that is an event. When a chart is resized, that is an event. Microsoft Excel defines a total of 63
different events. When an event occurs, programmers like to say that the event fires.
The purpose of an event is simply to allow the VBA programmer to write code that will execute
whenever an event fires. As we will see, this code is placed in an event procedure. The code itself
is referred to as event code. We wrote some simple event code for the Open and BeforeClose
workbook events when we created the SRXUtils add-in in the previous chapter.
Most Excel events break naturally into five groups, as indicated in Table 11-1 through Table 11-5.
These groups partially reflect the level at which the event takes place—the application level
(highest), the workbook level (middle), or the worksheet/chartsheet level (lowest).
To illustrate, when a worksheet is activated by the user or through code (by calling the Activate
method) several events will fire. They are, in firing order:
The Activate event of the worksheet. This event fires whenever the worksheet is activated.
The SheetActivate event of the workbook. This event fires whenever any worksheet in the
workbook is activated.
The SheetActivate event of the application. This event fires whenever any worksheet in
any workbook in the currently running instance of Excel is activated. (However, as we
will discuss later, to enable this event, we must write special code.)
11.1 The EnableEvents Property
It is important to note that no Excel event will fire unless the EnableEvents property is set to True
(although it is set to True by default). Thus, the programmer has control over whether Excel
events are enabled. The EnableEvents property is a property of the Application object, so, for
instance, to prevent the Save event from firing when the active workbook is saved, we can write:
Application.EnableEvents = False
Application.EnableEvents = True
11.2 Events and the Excel Object Model
The Excel object model contains several objects that exist simply as a convenience, in order to
include the Excel events in the object model. (We do not actually program with these objects.)
These objects are AppEvents, DocEvents, ChartEvents, WorkBookEvents, OLEObjectEvents, and
RefreshEvents. The events associated with a worksheet, for instance, are methods of the
DocEvents object, which is a child of the Worksheet object and the Chart object.
11.3 Accessing an Event Procedure
By now you are probably wondering how to write an event procedure. The short answer is that for
each event, Excel provides us with an event code shell where we can place the event code for that
To illustrate, consider the SelectionChange event of the Worksheet object. Figure 11-1 shows the
code window for a worksheet (Sheet1). Note that the Worksheet object is selected in the objects
list box. This causes the procedures list box to be filled with the names of the worksheet events.
We can simply choose the event for which we want to write event code.
Figure 11-1. Events for the Worksheet object
For instance, if we choose the SelectionChange event, Excel will automatically produce the
following code shell:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Excel will even place the cursor between the two code lines so we can begin entering event code.
As the name implies, this event fires when the current selection is changed in the worksheet. Note
that Excel will fill in the Target parameter with the Range object that represents the new
selection. Thus, our event code has access to the new selection, but not to the previous selection.
Many events have parameters associated with them. This provides a way for Excel to pass us
information related to the event.
The same approach will work for the workbook and chart events, but Application events require a
different approach, which we will discuss later in the chapter.
The Excel events are listed in Table 11-1 through Table 11-5.
11.4 Worksheet Events
The worksheet-related events are shown in Table 11-1. These events are also referred to as
Table 11-1. Worksheet Events (DocEvents)
Occurs when a worksheet is activated.
Occurs when a worksheet is double-clicked, before the default
PivotTableUpdate (Excel 10
Occurs when a worksheet is right-clicked, before the default rightclick action.
Occurs after the worksheet is recalculated.
Occurs when cells on the worksheet are changed by the user or by
an external link.
Occurs when the worksheet is deactivated.
Occurs after a PivotTable report is updated on a worksheet.
Occurs when the selection changes on a worksheet.
11.5 WorkBook Events
Table 11-2 shows the workbook-related events.
Table 11-2. Workbook Events
Occurs when a workbook is activated.
Occurs when the workbook is installed as an add-in.
Occurs when the workbook is uninstalled as an add-in.
Occurs before the workbook closes.
Occurs before the workbook (or anything in it) is printed.
Occurs before the workbook is saved.
Occurs when the workbook is deactivated.
Occurs when a new sheet is created in the workbook.
Occurs when the workbook is opened.
PivotTableCloseConnection (Excel 10 Occurs after a PivotTable closes the connection to its data
PivotTableOpenConnection(Excel 10 Occurs after a PivotTable opens the connection to its data
Occurs when any sheet is activated.
Occurs when any worksheet is double-clicked, before the
default double-click action.
Occurs when any worksheet is right-clicked, before the
default right-click action.
Occurs after any worksheet is recalculated or after any
changed data is plotted on a chart.
Occurs when cells in any worksheet are changed by the
user or by an external link.
Occurs when any sheet is deactivated.
Occurs when the selection changes on any worksheet
(does not occur if the selection is on a chart sheet).
Occurs when any workbook window is activated.
Occurs when any workbook window is deactivated.
Occurs when any workbook window is resized.