如何使用函数vba声明和填充二维数组

时间:2017-09-06 19:38:17

标签: arrays excel vba function

我有一个在子程序中声明的数组。我希望它充满两个值。一个单元格的每个坐标一个。我知道这并不总是最好的方法,但由于涉及我的其余程序的原因,我需要这样做。

'This sub contains more, I've just included only the relevant stuff
    Sub Start_Click()
        Dim arTime() As Integer
        arTime = FillTimeArray(arTime)
    End Sub


'This is used to fill the array with a list of 12 columns that are 24 rows long
'They are offsetted intentionally

    Function FillSpotArray(ByRef arr() As Integer) As Integer()
        For j = 0 To 11
            For i = 0 To 23
                arr(i, j) = Format(i + 2, j + 1)
            Next i
        Next j
    End Function

2 个答案:

答案 0 :(得分:0)

您尚未确定阵列的尺寸:

Sub Start_Click()
    Dim arTime(23, 11) As Integer
    arTime = FillTimeArray(arTime)
End Sub

答案 1 :(得分:0)

您需要对数组进行调暗,正确调用函数,并在函数

中正确返回输出
Sub Start_Click()
    Dim arTime As Variant
    arTime = FillSpotArray()
End Sub


'This is used to fill the array with a list of 12 columns that are 24 rows long
'They are offsetted intentionally

Function FillSpotArray() As Integer()
    Dim arr(23, 11) As Integer
    For j = 0 To 11
        For i = 0 To 23
            arr(i, j) = Format(i + 2, j + 1)
        Next i
    Next j
    FillSpotArray = arr
End Function

我不完全确定你在Format使用