Microsoft Office Tutorials and References
In Depth Information
USING THE MOUSEDOWN EVENT
Enabling Events for an Embedded Chart
Events are automatically enabled for Chart sheets, but not for charts embedded in a
worksheet. To use event procedures with an embedded chart, you need to perform the
following steps:
1. Create a class module. In the VB Editor window, select your project in the
Project window and select Insert
Class Module. This will add a new (empty)
class module to your project.
2. Declare a public Chart object. The variable should be of type Chart, and
it must be declared in the class module using the WithEvents keyword.
Following is an example of such a declaration:
Public WithEvents EmbChart As Chart
3. Connect the declared object with your chart . Do this by declaring an object
of type Class1 (or whatever your class module is named). This should be a
module-level object variable, declared in a regular VBA module (not in the
class module). Here’s an example:
Dim MyChart As New Class1
4. Execute a statement to instantiate the object. Example:
Set MyChart.EmbChart = ActiveSheet.ChartObjects(1).Chart
After the preceding statement is executed, the MyChart object in the class
module points to the first embedded chart on the active sheet. Consequently,
the event-handler procedures in the class module will execute when the
events occur.
5. Write event procedures for the chart class. These procedures go in the class
module. Here’s a simple example:
Private Sub EmbChart_Activate()
MsgBox EmbChart.Parent.Name & “ was activated.”
End Sub
6. Execute a statement to destroy the object and stop monitoring events.
Example:
Set MyChart.EmbChart = Nothing
All the examples that use Chart events are available in two versions on the CD-ROM:
one that uses a Chart sheet and one that uses an embedded chart.
Search JabSto ::




Custom Search