【问题标题】:Chart Event - Values not being picked up by code for more than 1 series of data图表事件 - 代码没有为超过 1 个数据系列拾取值
【发布时间】:2017-01-06 23:11:05
【问题描述】:

我试图构建一个交互式图表来表示一组数据。在下面的屏幕截图中,如果我们选择不同的按钮,我们可以为图表选择不同的数据系列。我的问题是,通过使用下面的代码,如果系列 1 为真,那么我能够获得 myX 和 myY 的值 ...对于选择的其他系列,我只会得到空白。

Private Sub Chart_MouseUp(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

        ' Did we click over a point or data label?
        If ElementID = xlSeries Or ElementID = xlDataLabel Then

        On Error Resume Next

            If Arg2 > 0 Then
                ' Extract x value from array of x values
                myX = WorksheetFunction.Index(.SeriesCollection(Arg1).XValues, Arg2)
                ' Extract y value from array of y values
                myY = WorksheetFunction.Index _
                    (.SeriesCollection(Arg1).Values, Arg2)


          MsgBox myX & ", " & myY

基本上是一行

 myX = WorksheetFunction.Index(.SeriesCollection(Arg1).XValues, Arg2)

如果选择系列 2 或 3,则抛出错误参数无效。如果选择了系列 1,则工作正常。

myX 为我提供点击国家的名称和 myY 的值。

视频链接可查看此 excel 文件中发生的情况。

Chart Template Video Youtube

【问题讨论】:

    标签: vba excel charts


    【解决方案1】:

    很傻,但是如果我们使用 FullSeriesCollection 而不是 SeriesCollection 就可以了。

    所以而不是行

    myX = WorksheetFunction.Index(.SeriesCollection(Arg1).XValues, Arg2)
    

    我必须使用:

    myX = WorksheetFunction.Index(.FullSeriesCollection(Arg1).XValues, Arg2)
    

    FullSeriesCollection的文档似乎给出了解释:

    FullSeriesCollection 对象 (Excel) 对象使您能够获得过滤掉的 Series Object (Excel) object 并将其过滤回。它还使您能够遍历整个系列对象集,过滤以编程方式显示或显示。通过让现有的SeriesCollection Object (Excel) 对象仅包含可见系列,您可以以编程方式仅对可见系列执行操作。它还可以防止 Microsoft Excel 破坏已过滤数据图表上的现有图表解决方案。

    【讨论】:

    • 查看FullSeriesCollection上的文档:
    • 感谢@DavidZemens ...现在我知道它为什么与 FullSeriesCollection 一起使用了。
    • 不客气!也恭喜您回答了您自己的问题,您也应该将其标记为“已接受”:)
    • 是的,我试过了......但它说你不能在 2 天内接受你自己的答案......也许在 2 天之后。 ☺
    • 哦,你是对的,你自己的问题有 2 天的等待时间,这是为了帮助防止垃圾邮件和其他不受欢迎的帖子。在此期间,我给了你一些支持。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 2020-12-05
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多