VBA 2-DIMENSIONAL DYNAMIC JAG ARRANGEMENT

Asked 1 years ago, Updated 1 years ago, 406 views

Purpose:
I want to create a 2D dynamic jug array
I want to expand only the second dimension later

We decided to declare a one-dimensional array in variant form and store it in it again.
I tried three methods, but all of them failed.
Please tell me the correct way.

Policy 1:
Extend the second dimension directly with redim

Submain()
    Dimarr(10) As Variant
    arr(0) = Array(1,2,3)
    redimarr(0)(3)'compilation error:syntax error
    arr(0)(4) = 4
End Sub

Policy 2:
Pass only the first dimension to another array and then expand

Submain()
    Dimarr(10) As Variant
    arr(0) = Array(1,2,3)
    Dimarr2
    arr2 = arr
    ReDimarr2(3)
    arr = arr2' compilation error: Cannot be assigned to array
End Sub

Policy 3:
Function the addition of elements

Submain()
    Dimarr(10) As Variant
    arr(0) = Array(1,2,3)
    Call push(arr(0), 4)'compilation error:type mismatch:please specify array or user-defined type
End Sub

Function push (arr() As Variant, v As Long)
    ReDimarr(UBound(arr)+1)
    arr(UBound(arr))=v
End Function

vba array

2023-03-06 15:24

1 Answers

"I think ""Policy 2"" would be fine."

Submain()

    Dimarr(10) As Variant
    Dimarr2
    
    arr(0) = Array(1,2,3)
    arr2 = arr(0)
    ReDimarr2(3)
    arr(0) = arr2
    
End Sub


2023-03-06 16:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.