【问题标题】:Get X Axis value on the Click of Chart - Excel VBA单击图表获取 X 轴值 - Excel VBA
【发布时间】:2013-03-29 13:12:26
【问题描述】:

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

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

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

请问有什么办法吗?

谢谢。

【问题讨论】:

    标签: vba excel charts


    【解决方案1】:

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

    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
    

    如果您的图表嵌入在工作表中,那么您将不得不使用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
    

    【讨论】:

    • 嗨,谢谢,但我认为它没有给出正确的 x 轴值。你能再看看吗?
    • 我可以看看你的示例文件吗?
    • 是的。 Here is the file 我按照您的指示在其中创建了图表表。 Link to File
    • 在我上面的帖子中查看后续内容
    • 嗨,无论我在哪里点击,我总是将 Arg2 设为
    猜你喜欢
    • 2017-06-13
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多