Chart object events

 1999-08-21    Events    0    199

If you want to write event procedures for an embedded chart, you will have to create a class module to do this:

  1. Start the Visual Basic Editor (press Alt+F11 in Excel).
  2. Select the desired project in the Project-window.
  3. Insert a new class module by selecting the menu Insert, Class Module.
  4. Activate the new class module and rename it, e.g. ChartEventClass.
  5. Copy and paste the example macro below to the new class module.
Public WithEvents ChartObject As Chart

Private Sub ChartObject_Select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long)
    ActiveChart.Deselect
End Sub

' add more macros for the other available chart events if necessary...
After you have finished editing the event macros for the Chart object, you have to add some code to the module ThisWorkbook to activate the new event macros:

Dim ChartObjectClass As New ChartEventClass

Private Sub Workbook_Open()
    Set ChartObjectClass.ChartObject = Worksheets(1).ChartObjects(1).Chart
End Sub
After the Workbook_Open procedure has run, the events attached to the first Chart object in the first worksheet will be activated.