2014-01-29 20 views
0

每個元素我已經得到了我想要把圖表中的數據的一個比較大的量。Excal宏觀循環在陣列

基本上,我是有多個部件(總共30上下的但在下面的例子中,我會限制碼至5層的組件),並且每個部件具有一列。 我已將列號分配給各個組件,聲明爲整數。 有時候我不希望組件有一個圖表因此,這就是有一個在數字的差距(例如在DIAT和HYDR之間是5列中的另一組成部分,但爲這一個不應該有一個圖)。

然後,我希望把我全部想要的圖形成一個陣列,並做了For ... Next循環這樣的圖形將自動對每一個單元陣列中創建(所以在每個組件的組件數組)。

很顯然,我做錯了什麼:-)。該代碼被卡住在第一時間我嘗試指推崇元件陣列中:ActiveChart.SeriesCollection(1)請將.Name =細胞(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的經驗是有限的,所以你們誰都知道如何解決這個問題? 在此先感謝!

回答

0

Array()創建一個下限爲零的單維數組(除非您在模塊的頂部使用Option Base 1)。

所以你應該循環從0到ubound(componentlist)-1,你只需要使用componentlist(i),因爲你的數組只有一個維度。

+0

謝謝!工作:) – blaaat

0

您應該研究的第一件事是:如果需要,是否LBOUND(componentlist)爲1或0並修復for循環。

接下來的事情來解決是componentlist的維度。它應該是單一的。

+0

與上述相同的答案,作品:-)。乾杯! – blaaat