首页 > 解决方案 > 获取活动图表中活动系列的数量

问题描述

我正在尝试制作一个相当简单的宏来激活已经活动图表中的下一个系列。(我的主宏包含所有格式化的东西)。这种编码的问题是我无法分配当前的活动系列(已经在 excel 的图表中选择)。在具有 n 个系列 (SeriesCollection.Count = n) 的图中,我想从 x 到 x+1,如果 x=ni 想回到系列 1(所以如果我得到,这里包含的 for/next 不是必需的宏按预期工作)。

Sub NextButton_Click()

Dim cht As Chart
Set cht = ActiveChart

If cht Is Nothing Then
MsgBox "Select a chart."
Exit Sub
End If

   With cht

       For SrsIndx = 1 To .SeriesCollection.Count
       .SeriesCollection(SrsIndx).Select
       Next SrsIndx

   End With

End Sub

标签: excelvba

解决方案


对于简单图,只需使用PlotOrder:如果您不使用组合图(即不混合折线图和条形图,也不使用辅助轴),那么您可以只使用以下代码:

IIF(TypeName(Selection)="Series", Selection.PlotOrder, -1)

-1如果您没有选择,这将返回Series

然而,这实际上是在ChartGroup- 上面的条件是当 . 上只有 1 时ChartGroup的顺序Chart

否则,请尝试使用Name和循环:

Function ActiveSeriesNumber(ThisSeries AS Series) AS Long
    Dim ThisChart AS Chart, TestNumber AS Long
    ActiveSeriesNumber = -1
    On Error GoTo FunctionError

    Set ThisChart = ThisSeries.Parent.Parent 'Object Model Is Chart.ChartGroup.Series
    For TestNumber = 1 to ThisChart.SeriesCollection.Count
        If ThisChart.SeriesCollection(TestNumber).Name = ThisSeries.Name THen
            ActiveSeriesNumber = TestNumber
            Exit Function
        End If
    Next TestNumber

FunctionError:
    On Error GoTo -1
End Function

通过调用使用它ActiveSeriesNumber(Selection)


推荐阅读