单击图表时获取 X 轴值 - Excel VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15703967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:19:42  来源:igfitidea点击:

Get X Axis value on the Click of Chart - Excel VBA

vbaexcel-vbachartsexcel

提问by Tejas

I have come across a strange requirement.

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

I need to get X-Axis value from chart when user clicks on Chart Area.

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

I know we can assign a macro to a chart. So in way, event for the chart can be created. But no idea of how to proceed further.

我知道我们可以为图表分配一个宏。因此,可以为图表创建事件。但不知道如何进一步进行。

Any idea to do this please?

有什么想法可以这样做吗?

Thanks.

谢谢。

回答by Siddharth Rout

If your Chart is in a Chart Sheet then you can right click on the Chart sheet tab, select "View code" and paste the following in its code module (see screenshot below)

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

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

在此处输入图片说明

If your chart is embedded in a sheet then you will have to use WithEventsas @brettdj has already covered here

如果您的图表嵌入在工作表中,那么您将不得不使用WithEvents@brettdj 已在此处介绍的内容

FOLLOWUP

跟进

Is this what you are trying?

这是你正在尝试的吗?

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

在此处输入图片说明