Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA - Add 1-dimensional array to multi-dimensional array without looping

I have a question regarding "creating a matrix" out of single arrays wihtout having to loop through:

From a function I get back one array with data (Return_Calc, rows = n-1). I am looking for something like

    Output(n-1, j-1) = Return_Calc(Nav_Range)

At the moment, I am doing this:

    Temp = Return_Calc(Nav_range)

    For i = 1 To n - 1

        Output(i - 1, j - 1) = Temp(i - 1)

    Next i 

The current option works. I was just wondering if there is another possibility without looping. Thanks for your help!

like image 246
Dani Avatar asked May 27 '26 13:05

Dani


1 Answers

I'm not sure if you would be happy with that proposal. It presents possibility of creating Array-of-Arrays which, in some situation, would work similar to Multidimensional Array. You could consider solving your problems this way.

Here is a sample code how to create and which way you could retrieve data from final array.

Sub Array_Workaround()

    Dim oneDimArrA, oneDimArrB
        oneDimArrA = Array(1, 2, 3, 4)
        oneDimArrB = Array("A", "B", "C", "D")
    Dim multiDimArr

    'creating multidemmnsional array
    multiDimArr = Array(oneDimArrA, oneDimArrB)

    'get element- different to standard syntax
    Debug.Print multiDimArr(0)(0)   '--> 1
    Debug.Print multiDimArr(0)(1)   '--> 2
    Debug.Print multiDimArr(1)(1)   '--> B

End Sub

There is one important benefit of presented solution- each internal array can have different dimension.

like image 126
Kazimierz Jawor Avatar answered May 30 '26 06:05

Kazimierz Jawor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!