Arrays 数组中每个元素的Excal宏循环

Arrays 数组中每个元素的Excal宏循环,arrays,excel,vba,for-loop,Arrays,Excel,Vba,For Loop,我有一个比较大的数据量,我想放在图表中 基本上,我有多个组件,总共30个,但在下面的示例中,我将代码限制为5个组件,每个组件有一列。 我已经将列编号分配给各个组件,声明为整数。 有时,我不希望一个组件有一个图表,因此这就是数字之间存在差距的地方,例如diat和hydr之间是第5列中的另一个组件,但对于这个组件,不应该有一个图表 然后我想把我想要一个图形的所有组件放在一个数组中,并为。。。下一个循环将自动为数组中的每个元素创建一个图形,从而为数组中的每个组件创建一个图形 显然我做错了什么:-。当我

我有一个比较大的数据量,我想放在图表中

基本上,我有多个组件,总共30个,但在下面的示例中,我将代码限制为5个组件,每个组件有一列。 我已经将列编号分配给各个组件,声明为整数。 有时,我不希望一个组件有一个图表,因此这就是数字之间存在差距的地方,例如diat和hydr之间是第5列中的另一个组件,但对于这个组件,不应该有一个图表

然后我想把我想要一个图形的所有组件放在一个数组中,并为。。。下一个循环将自动为数组中的每个元素创建一个图形,从而为数组中的每个组件创建一个图形

显然我做错了什么:-。当我第一次尝试引用数组中受尊重的元素时,代码就卡住了:ActiveChart.SeriesCollection1.Name=Cells9,componentlist1,I

我使用VBA的经验有限,所以你们中有人知道如何解决这个问题吗? 提前谢谢

Array创建下限为零的一维数组,除非您在模块顶部使用选项Base 1


因此,您应该从0循环到uboundcomponentlist-1,并且您只需要使用componentlisti,因为您的数组只有一个维度。

您应该调查的第一件事是LBOUNDcomponentlist是1还是0,并在必要时修复for循环

接下来要解决的是componentlist的维度。它应该是一维的

Dim diat, hydr, para, terb, theo As Integer
diat = 4        'column number of the component named diat
hydr = 6        'column number of the component named hydr
para = 7        'column number of the component named para
terb = 9        'column number of the component named terb
theo = 10       'column number of the component named theo

Dim componentlist As Variant
componentlist = Array(diat, hydr, para, terb, theo)

For i = 1 To UBound(componentlist)

ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterLines
ActiveChart.SeriesCollection.NewSeries.Select
ActiveChart.SeriesCollection(1).Name = Cells(9, componentlist(1, i))
ActiveChart.SeriesCollection(1).XValues = Worksheets(2).Range(Cells(10, 3), Cells(21, 3))
ActiveChart.SeriesCollection(1).Values = Worksheets(2).Range(Cells(10, componentlist(1, i)), Cells(21, componentlist(1, i)))

Next