Adding to an Array function in Excel VBA

后端 未结 1 498
旧时难觅i
旧时难觅i 2021-01-27 20:27

I am trying to add an array to an array of Double arrays in a for loop. Here is the code I have:

Sub Test3()
 Dim a() As Double, i As Integer
 ReDim a(1 To 10,          


        
1条回答
  •  不思量自难忘°
    2021-01-27 20:43

    You want what's called a "Jagged Array", or Array of Arrays

    Try this

    Sub Demo()
        Dim a() As Double, b() As Double, c() As Double, i As Integer
    
        ReDim a(1 To 10, 1 To 3)
        ReDim b(1 To 2, 1 To 4)
        ReDim c(1 To 5, 1 To 11)
    
        a(1, 2) = 3.5
        b(1, 2) = 2.5
    
        Dim d As Variant
        ReDim d(1 To 6)
    
        ' Add 3 copies of a to d
        For i = 1 To 3
         d(i) = a
        Next i
    
        ' add other arrays to d
        d(4) = b
        d(5) = c
    
        ' Access elements of d
        Dim x() As Double
        x = d(1)
    
        MsgBox x(1, 2)
        MsgBox d(1)(1, 2)
        MsgBox d(4)(1, 2)
    End Sub
    

    0 讨论(0)
提交回复
热议问题