9

I need to be able to create a newarray and assign it to another2darray(atsomeelement)

Example

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

and now

array2(0) = array1

and therefore

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

Now I want to take make a new array and assign 1d of the array2 to it.

newarray = array2(0)

and therefore

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

I cannot do this in VBA code.
Code snippet below, works if you comment out the last section where I try and assign array2(1) to 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
BigBen
  • 46,229
  • 7
  • 24
  • 40
Aden
  • 91
  • 1
  • 3
  • See http://stackoverflow.com/q/8482759/641067 – brettdj Jan 13 '12 at 06:30
  • 1
    Note that, technically, what you have is *not* a 2d array but an array of arrays. That's something different. (There are also real 2d arrays in VBA.) – Heinzi Jan 13 '12 at 09:55

1 Answers1

8

You cannot assign to a fixed-size array. Declare it as a dynamic one.

Dim arraynew() As String
arraynew = array2(1) 
Fionnuala
  • 90,370
  • 7
  • 114
  • 152
GSerg
  • 76,472
  • 17
  • 159
  • 346