Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill multidimensional array

Tags:

excel

vba

I was wondering how to fill a multi-dimensional array in Excel VBA. A 1d array can be filled as follows:

Dim myarray as variant

myarray = Array("sheep", "goat", "chicken")

How would I fill each row separately for a multi-dimensional array?

like image 299
Regenbui Avatar asked Nov 07 '14 18:11

Regenbui


People also ask

How do you fill a multi dimensional array in Java?

We can use a loop to fill a multidimensional array. // given value. int [][]ar = new int [ 3 ][ 4 ]; // Fill each row with 10.

Does arrays fill work on 2D arrays?

The recommended approach to initialize an array with any value is using the Arrays. fill() method. For a 2D array, Arrays. fill() can be called for each array using a for-loop.


1 Answers

You can do this:

Dim a
a = [{1,2;3,4;5,6}]

Limitations:

  • This only works with arrays of type Variant, because [x] is shorthand for Evaluate("x") which means that x is interpreted via Excel, and Excel only returns Variants. So declaring Dim a As Variant or an array Dim a() As Variant works fine. But not any other type of array e.g. Dim a() As String fails.

  • This only works for one specific kind of multi-dimensional array, namely two-dimensional arrays. Not three-, four- etc. dimensional.

like image 97
Tim Williams Avatar answered Nov 09 '22 16:11

Tim Williams