【问题标题】:Return index of selected series所选系列的返回索引
【发布时间】:2015-02-13 08:34:26
【问题描述】:

有没有一种简单的方法可以使用 VBA 在 Excel 图表中获取选定行的索引?我有一个图表,用户在其中选择一个系列。然后宏应该做一些事情。我正在寻找类似idx = Selection.getIndex 的东西。

我需要这个 idx 来调用其他函数,这些函数使用索引来选择特定的系列(例如 FullSeriesCollection(idx).DataLabels.labelPos=...

【问题讨论】:

    标签: excel vba indexing charts line


    【解决方案1】:

    给你三个想法:

    1st-使用对象变量代替索引引用:

    Dim SER As Series
    Set SER = Selection
    SER.anyproperty.anymethod... 'do your action here
    

    第二次使用绘图顺序来获取...索引(但不确定这是否总是匹配)?

    Dim inxSer As Integer
    inxSer = Selection.PlotOrder
    

    第三次读取序列公式的最后一个参数

    Dim inxFromFormula As Integer
    Dim tmpSerFormula As String
    tmpSerFormula = Selection.Formula
    
    tmpSerFormula = Mid(tmpSerFormula, InStrRev(tmpSerFormula, ",") + 1)
    inxFromFormula = Left(tmpSerFormula, Len(tmpSerFormula) - 1)
    

    【讨论】:

    • 这里的第一个选项可能是最健壮和最有用的。
    【解决方案2】:

    您可以利用图表对象事件来实现这一点。如果图表嵌入在工作表中,则需要首先将其声明为“WithEvents”变量,如下所述: Using Events with Embedded Charts

    之后,您可以使用以下参数定义 SeriesChange 处理程序:

    Private Sub myChartClass_SeriesChange(ByVal SeriesIndex As Long, ByVal PointIndex As Long)
    
    End Sub
    

    编辑:当用户更改点值时会触发上述事件,您应该改用 Select 事件。

    myChartClass_Select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long) 
    

    Parameter description (the same as BeforeDoubleClick)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-23
    • 2020-04-19
    • 2018-02-09
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    相关资源
    最近更新 更多