Chart object events using VBA in Microsoft Excel

If you want to write event procedures for an embedded chart, you will have to create a class module to do this: Start the Visual Basic editor.
Select the desired project in the Project-window.
Insert a new class module by selecting the menu Insert | Class Module.
Activate the new class module and rename it, e.g. ChartEventClass
Copy and paste this example macro 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 you run the Workbook_Open procedure, the events attached to the first Chart object
in the first worksheet are activated.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.

Visit Us On TwitterVisit Us On FacebookVisit Us On Google PlusVisit Us On Youtube