如何在VBA中将2d数组的一维分配给新的1d数组

时间:2012-01-13 00:09:25

标签: arrays visual-studio ms-access vba

我需要能够创建一个newarray并将其分配给another2darray(atsomeelement)

实施例

array1(0) = 1
array1(1) = 2

现在

array2(0) = array1

因此

array2(0)(0) = 1
array2(0)(1) = 2

现在我想创建一个新数组并将array1的1d分配给它。

newarray = array2(0)

因此

newarray(0) = 1
newarray(1) = 1

我无法在VBA代码中执行此操作 下面的代码段,如果您注释掉我尝试的最后一部分,并将array2(1)分配给arraynew,则会有效。

Function test()
    Dim array1(0 To 20) As String
    Dim array2(0 To 5) As Variant
    Dim count As Integer

    For count = 0 To UBound(array1)
     array1(count) = count
    Next count

    'now that array1 is filled i want to insert it into array2(1)
    array2(1) = array1

    ' test
    MsgBox (array2(1)(3))

    'now i want to create a new string array and assign it to array2(1)
    Dim arraynew(0 To 20) As String
    arraynew = array2(1)
    'this is what fails. 

End Function

1 个答案:

答案 0 :(得分:7)

您无法分配固定大小的数组。将其声明为动态的。

Dim arraynew() As String
arraynew = array2(1)