excel - 获取活动图表中活动系列的数量
问题描述
我正在尝试制作一个相当简单的宏来激活已经活动图表中的下一个系列。(我的主宏包含所有格式化的东西)。这种编码的问题是我无法分配当前的活动系列(已经在 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
解决方案
对于简单图,只需使用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)
推荐阅读
- android - 键预期 String[] 但值是 java.lang.String
- python - 查找 3D 三角形的第三个点
- powershell - 由于 unicode 字符,Powershell 脚本无法运行
- python - 为什么我的 numpy 构造忽略元组解构?
- arrays - 如何在 vb.net 的 my.settings 中存储一个数组
- reactjs - 如何在 Material UI 中的 Avatar 组件上使用阴影
- python - 如果一列在数据框中有多个值,则创建多行
- xcode - XCode 12 中单元测试用例的顺序是什么?
- javascript - 如何解决错误:ENOENT:没有这样的文件或目录
- r - 为什么 rbind 在用户定义的函数中不起作用?