我有一個表格(表格1),我用這個表格在同一圖紙 - 嵌入圖表中創建了散點圖(「圖表標題」)。我想選擇一個數據點並檢索值。我在網上找到了以下內容。選擇並檢索數據點 - 嵌入圖表-VBA
Dim myClassModule() As New EventClassModule
Sub InitializeChart()
If ActiveSheet.ChartObjects.Count > 0 Then
ReDim myClassModule(1 To ActiveSheet.ChartObjects.Count)
Dim chtObj As ChartObject
Dim chtnum As Integer
For Each chtObj In ActiveSheet.ChartObjects
chtnum = chtnum + 1
Set myClassModule(chtnum).myChartClass = chtObj.Chart
Next
End If
End Sub
Sub ResetCharts()
Dim chtnum As Integer
For chtnum = 1 To UBound(myClassModule)
Set myClassModule(chtnum).myChartClass = Nothing
Next
End Sub
這正好類模塊
Public WithEvents myChartClass As Chart
Private Sub myChartClass_Mousedown(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
Dim ElementID As Long, Arg1 As Long, Arg2 As Long
Dim myX As Variant, myY As Double
With ActiveChart
.GetChartElement x, y, ElementID, Arg1, Arg2
If ElementID = xlSeries Or ElementID = xlDataLabel Then
If Arg2 > 0 Then
myX = WorksheetFunction.Index _
(.SeriesCollection(Arg1).XValues, Arg2)
myY = WorksheetFunction.Index _
(.SeriesCollection(Arg1).Values, Arg2)
MsgBox (Arg1 & Chr(10) & Arg2)
End If
End If
End With
End Sub
但是下面一行:
Dim myClassModule() As New EventClassModule
導致的錯誤:
Used - defined type not defined & Cannot perform requested operation
任何想法,爲什麼?
類模塊必須命名爲EventClassModule。 轉到vbe,在左上角項目exlorer clic上的類 ,然後在屬性窗口中右鍵單擊(名稱),並將名稱更改爲EventClassModule。 –
也不知道爲什麼在模塊中他們使用'withactivechart',而不是使用myChartClass的專有名稱' –
你是什麼意思? – Jordan