返回所选系列的索引

问题描述 投票:0回答:2

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

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

excel vba indexing charts line
2个回答
2
投票

三个想法:

1st-使用对象变量而不是索引引用:

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

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

Dim inxSer As Integer
inxSer = Selection.PlotOrder

3-读取系列公式的最后一个参数

Dim inxFromFormula As Integer
Dim tmpSerFormula As String
tmpSerFormula = Selection.Formula

tmpSerFormula = Mid(tmpSerFormula, InStrRev(tmpSerFormula, ",") + 1)
inxFromFormula = Left(tmpSerFormula, Len(tmpSerFormula) - 1)

1
投票

您可以使用Chart对象事件来实现这一点。如果图表嵌入在工作表中,则需要首先将其声明为“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)

© www.soinside.com 2019 - 2024. All rights reserved.