我正在尝试编写一个循环遍历预定义图表名称数组的代码,并对每个图表进行smth 我最初的猜测是
Sub Select_Charts_On_Sheet()
'Selects every chart on a sheet
Dim c As Chart
Dim ChartArray As Variant
ChartArray = Array("Chart 1", "Chart 2", "Chart 3", "Chart 4")
For Each c In Charts(ChartArray)
MsgBox (c.Name)
Next c
End Sub
然而它不起作用。有什么建议吗?
答案 0 :(得分:1)
您需要循环浏览工作表ChartObjects
,并检查每个ChartObject.Name
是否在ChartArray
内找到。
<强>代码强>
Option Explicit
Sub Select_Charts_On_Sheet()
'Selects every chart on a sheet
Dim ChtObj As ChartObject
Dim ChartArray As Variant
ChartArray = Array("Chart 1", "Chart 2", "Chart 3", "Chart 4")
' loop through worksheet's chart objects
For Each ChtObj In Worksheets("Sheet1").ChartObjects ' modify "Sheet1" with your sheet's name
MsgBox ChtObj.Name
' use application match to see if current chart object name is inside the ChartArray (of names)
If Not IsError(Application.Match(ChtObj.Name, ChartArray, 0)) Then ' Match was successful
ChtObj.Select
End If
Next ChtObj
End Sub
答案 1 :(得分:0)
要在工作表上执行每个图表,这很简单:
Dim ChOb As ChartObject
For Each ChOb In ActiveSheet.ChartObjects
With ChOb.Chart
' do something to each chart
End With
Next
可替换地:
Dim iCht As Long
For iCht = 1 To ActiveSheet.ChartObjects.Count
With ActiveSheet.ChartObjects(i).Chart
' do something to each chart
End With
Next