matt7
matt7

Reputation: 11

Why does my embedded chart cause Excel to crash if I don't open VBA first?

I have an embedded chart on a protected Excel sheet that I would like to process chart events on (MouseMove and MouseDown), but Excel crashes when I click on the chart unless I first open the VBA editor. If I don't open VBA and click on the chart, Excel crashes. If I open VBA and click on the chart, the mouse events work as expected.

I would like to be able to open the workbook and see chart events work without having to open and close the VBA editor first.

I was able to replicate the problem in a new workbook:

I created a new workbook and added new empty line chart with the name MyChart.

In VBA, I created a new class module named clsChartEvents with the following contents:

Public WithEvents ChartClass As Chart

Private Sub ChartClass_MouseMove(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
    Worksheets("Sheet1").Range("E3").Value = Button
    Worksheets("Sheet1").Range("F3").Value = Shift
    Worksheets("Sheet1").Range("G3").Value = x & ", " & y
End Sub

Private Sub ChartClass_MouseDown(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
    Worksheets("Sheet1").Range("E2").Value = Button
    Worksheets("Sheet1").Range("F2").Value = Shift
    Worksheets("Sheet1").Range("G2").Value = x & ", " & y
End Sub

And I added the following to the ThisWorkbook VBA module:

Dim modChartEvents As New clsChartEvents

Private Sub Workbook_Open()

    Dim chtObj As ChartObject
    Set chtObj = Worksheets("Sheet1").ChartObjects("MyChart")
    
    Worksheets("Sheet1").Unprotect
    
    chtObj.Locked = False
    chtObj.Chart.ProtectSelection = True
    chtObj.ProtectChartObject = True
    Set modChartEvents.ChartClass = chtObj.Chart
    
    Worksheets("Sheet1").Protect UserInterfaceOnly:=True

End Sub

After saving, closing, and reopening the workbook, I'm expecting to see the button, shift, and x,y info displayed in Sheet1 when I click on the chart and move my mouse around. Instead, Excel crashes. However, it does work as expected if I open VBA editor first (either leaving it open or immediately closing it).

Update 4/12: I moved the code I had in Workbook_Open to a new Public Sub named InitChartEvents and put the inverse of it in Workbook_Open, to "revert" the chart attributes on Open:

chtObj.Locked = True
chtObj.Chart.ProtectSelection = False
chtObj.ProtectChartObject = False
Set modChartEvents.ChartClass = Nothing

After saving and reopening the workbook, Excel does not crash when I click on the chart (because its attributes are reverted to defaults on open). A few different things I tested:

The button and selection change event were tests to see if the chart or chart object weren't being initialized/loaded until after the Workbook_Open ran. These let me trigger the same code by an action I could take after fully opening the workbook.

I'm puzzled and left wondering what is special about opening VBA or using the Macros window to run the code.

Upvotes: 1

Views: 235

Answers (0)

Related Questions