2016-04-05 114 views
1

所以我有一個1000行10列的2d陣列。我想爲10列中的每一列創建10個範圍,然後將它們用作系列來製作圖表。我的問題是,如何在數組中定義一個範圍,而不使用整個數組,而只使用一列?VBA定義陣列的範圍

Sub ChartLord() 

    Dim rows As Long 
    Dim columns As Integer 
    Dim mychart As Chart 
    Dim data As Range 
    Dim dataset() As Double 

    rows = ShData.Cells(ShData.rows.Count, 1).End(xlUp).Row 
    columns = ShData.Cells(1, ShData.columns.Count).End(xlToLeft).Column 

    'set array range (includes column titles and xAxix column) 
    dataset = ShData.Range(ShData.Cells(1, 1), ShData.Cells(rows, columns)) 



    For Z = 0 To 10 

     Set data = ? 
     Set mychart = shtCharts.Shapes.AddChart2(200, xlColumnClustered, 50 + 300 * Z, 50, 300, 200, 5).Chart 


    Next Z 



End Sub 
+1

您應該明白,**行**和**列**都是VBA中的保留字,不應用於命名變量。 – Jeeped

回答

4

下面將要收集範圍的值成2D範圍然後第二列切掉到一個新的2D陣列。後者只是1到1'列'寬。

Dim rws As Long, cols As Long, d As Long 
Dim dataset() As Variant, subdataset() As Variant 
Dim ShData As Worksheet 

Set ShData = Worksheets("Sheet4") 

rws = ShData.Cells(ShData.rows.Count, 1).End(xlUp).Row 
cols = ShData.Cells(1, ShData.columns.Count).End(xlToLeft).Column 

'set array range (includes column titles and xAxix column) 
dataset = ShData.Range(ShData.Cells(1, 1), ShData.Cells(rws, cols)) 

subdataset = Application.Index(dataset, 0, 2) '<~~second column 
For d = LBound(subdataset, 1) To UBound(subdataset, 1) 
    Debug.Print subdataset(d, 1) 
Next d 

ShData.Cells(1, "Z").Resize(UBound(subdataset, 1), UBound(subdataset, 2)) = subdataset 

最後一個操作將剝離的列的值放回到從Z1開始的工作表中。

+0

謝謝你,你願意在本網站的聊天論壇上與我聊天嗎?我只是有幾個關於vba繪圖系列的問題。 – Duranchula