在Click of Chart - Excel VBA上获取X轴值

时间:2013-03-29 13:12:26

标签: vba excel-vba charts excel

我遇到了一个奇怪的要求。

  

当用户点击图表区域时,我需要从图表中获取X轴值。

我知道我们可以为图表指定一个宏。因此,可以创建图表的事件。但不知道如何继续前进。

有任何想法吗?

感谢。

1 个答案:

答案 0 :(得分:4)

如果您的图表位于图表工作表中,则可以右键单击图表工作表选项卡,选择“查看代码”并将以下内容粘贴到其代码模块中(参见下面的屏幕截图)

Option Explicit

Private Sub Chart_Select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long)
    Select Case ElementID
    Case xlSeries
       If Arg2 > 0 Then
           MsgBox "Series " & Arg1 & vbCr & "Point " & Arg2
       End If
    Case Else
       MsgBox Arg1 & vbCr & Arg2
    End Select
End Sub

enter image description here

如果您的图表已嵌入工作表中,则必须使用WithEvents,因为@brettdj已涵盖here

<强>后续

这是你在尝试的吗?

Private Sub Chart_Select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long)
    Select Case ElementID
    Case xlSeries
        If Arg2 > 0 Then
            x = ActiveChart.SeriesCollection(Arg1).XValues
            For i = LBound(x) To UBound(x)
                If i = Arg2 Then
                    MsgBox "Point :" & i & "and X Value = " & x(i)
                    Exit For
                End If
            Next i
       End If
    End Select
End Sub

enter image description here