[英]Excal macro loop for each element in array
我想將大量數據放入圖表中。
基本上,我所擁有的是多個組件(總共30塊,但是在下面的示例中,我將代碼限制為5個組件),每個組件都有一列。 我已將列號分配給聲明為整數的各個組件。 有時我不希望某個組件具有圖形,所以那是數字之間的差距(例如,在diat和hydr之間是第5列中的另一個組件,但是對此不應該有一個圖形)。
然后,我想將需要圖形的所有組件放到一個數組中,然后執行For ... Next循環,這樣將為數組中的每個元素(因此為數組中的每個組件)自動創建一個圖形。
顯然我做錯了:-)。 我第一次嘗試引用數組中受關注的元素時,代碼卡住了:ActiveChart.SeriesCollection(1).Name = Cells(9, componentlist(1,i) )
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
我在VBA方面的經驗有限,所以你們當中有人知道如何解決嗎? 提前致謝!
Array()
創建一個下限為零的一維數組(除非您在模塊頂部使用Option Base 1
)。
因此,您應該從0循環到ubound(componentlist)-1
並且只需要使用componentlist(i)
因為您的數組只有一個維度。
您應該調查的第一件事是LBOUND(componentlist)是1還是0,並在必要時修復for循環。
接下來要解決的是componentlist的維度。 它應該是一維的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.