在VBA中,如何让“ActiveChart.FullSeriesCollection(i).Values”指向二维数组的特定行?

时间:2016-10-22 16:22:56

标签: arrays excel vba graph

我有一个大的2维数组(600行x 18列)。我可以使我的图形“ActiveChart.FullSeriesCollection(i).Values”=单维数组名称OK。

但是,当我引用二维数组的名称时,它总是默认拾取顶行。有没有办法让它选择我选择的行? 我尝试过以下方法: ActiveChart.FullSeriesCollection(i).Values = arrayname(25,) 但没有运气。 这可能吗?

1 个答案:

答案 0 :(得分:0)

您有更多选择:

1)使用函数检索想要的行并将其作为一维数组返回

Function Get2DArrayRow(arr2D As Variant, irow As Long) As Variant
    ReDim rowArr(LBound(arr2D, 2) To UBound(arr2D, 2)) As Variant
    Dim i As Long, j As Long

    For j = LBound(arr2D, 2) To UBound(arr2D, 2)
        rowArr(j) = arr2D(irow, j)
    Next j
    Get2DArrayRow = rowArr
End Function

被利用为

ActiveChart.FullSeriesCollection(1).Values = Get2DArrayRow(arrayname, 3) '<-- retrieve 3rd row of your 2D array

2)使用锯齿状数组

你的大型2D阵列是否来自excel范围

Sub main()
    Dim arr() As Variant '<-- jagged array
    Dim i As Long
    With Range("A1:Z1000") '<-- reference the range you want to fill 2D array with 
        ReDim arr(1 To .Rows.Count) '<-- size your jagged array to match rows number only        
        For i = 1 To .Rows.Count '<-- loop through your referenced range rows
            arr(i) = Application.Transpose(Application.Transpose(.Rows(i).Value)) '<-- fill jagged array current element with a 1D array
        Next i
    End With

    ActiveChart.FullSeriesCollection(1).Values = Application.Transpose(arr(25)) '<-- use 25th element of your jagged array
End Sub