我创建了一个类模块,用于捕获图表对象上的点击事件。它对于一个图表按预期工作,但是当我有多个图表时,只有最后创建的图表保持图表类的功能。下面是我用来将图表类应用于每个图表对象的代码。
Option Explicit
Global gclsDrill As New clsDrill
'Purpose: Initialize the Drill down class on each chart object
Sub InitChart()
Dim oChtObj As ChartObject
Dim oWorksheet As Worksheet
If oWorksheet Is Nothing Then Set oWorksheet = Sheets("Charts")
For Each oChtObj In oWorksheet.ChartObjects
Set gclsDrill.Chart = oChtObj.Chart
Next
End Sub
我不确定为什么它仅适用于一个图表对象,而不是每个对象都无法找到任何相关信息。有人有主意吗?感谢。
答案 0 :(得分:0)
不使用类的全局实例,而是使用Collection:为每个图表创建类的新实例,并将每个实例添加到Collection。
Option Explicit
Global colCls As Collection 'clsDrill
'Purpose: Initialize the Drill down class on each chart object
Sub InitChart()
Dim oChtObj As ChartObject
Dim oWorksheet As Worksheet
Set colCls = New Collection
If oWorksheet Is Nothing Then Set oWorksheet = Sheets("Charts")
For Each oChtObj In oWorksheet.ChartObjects
colCls.Add GetHandler(oChtObj.Chart)
Next
End Sub
Function GetHandler(cht As Chart) As clsDrill
Dim rv As New clsDrill
Set rv.Chart = cht
Set GetHandler = rv
End Function