Initializing Multidimensional Array at Once

kpark91

Well-known Member
Joined
Jul 15, 2010
Messages
1,582
Hello, MrExcel!

One simple questions about VBA arrays.
Is there a way I can initialize all the elements inside an array at once, specifically multidimensional array?

For example, in C/C++
Code:
int c[2][5] =
{
{1,2,3,4,5},
{2,3,4,5,6},
};

What about in VBA??
Code:
Dim arr(1 To 10, 1 To 3) As Integer

All I know for initializing a VBA array is by setting value for every single element separately.
I.e:
Code:
arr(1,1) = 256
arr(1,2) = 244
arr(1,3) = 8

Is there a short way of doing this?
Otherwise, I will need 30 lines;;; which is... bad;;

Thank you in advance!
kpark
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Found the solution:
http://www.mrexcel.com/forum/showthread.php?18225-Initializing-Arrays-with-a-single-statement

Too bad, the variable needs to be first declared as a variant.. I don't like that datatype altho it can be convenient at times.

Well, the array could be declared as String and populated the way I showed in my posting to that thread... VB will be happy to coerce your "text numbers" to real numbers when you involve the members of the array in calculations.
 
Upvote 0
kpark

I don't see a post for populating a multi-dimensional array in that thread.

Can you post the method you are using?
 
Upvote 0
Code:
    Dim av As Variant
    
    av = [{1,2,3;4,5,6;7,8,9}]
 
Upvote 0
Well, the array could be declared as String and populated the way I showed in my posting to that thread... VB will be happy to coerce your "text numbers" to real numbers when you involve the members of the array in calculations.

Variant magic never ceases to amuse me :)
Thank you!

kpark

I don't see a post for populating a multi-dimensional array in that thread.

Can you post the method you are using?

I just used combination of Array() and then used it like arr(i)(j) to look into the values.

Code:
    Dim av As Variant
     
     av = [{1,2,3;4,5,6;7,8,9}]

!!!!!! Another black variant magic. Nice to know! Will use this method :)
Thank you!
 
Upvote 0
Not so magic; it works the same as if you selected a 3x3 array of cells and array-entered ={1,2,3;4,5,6;7,8,9}

The brackets are just a shortcut to the Evaluate function.
 
Upvote 0
Code:
c=Array( _
    Array(1,2,3,4,5), _
    Array(2,3,4,5,6))
You have posted a response to a nearly 6 year old question. What I cannot figure out is if you are trying to offer a solution with the code you posted or if you have a question related to the topic of this thread and forgot to include a worded description of your question.
 
Upvote 0
You have posted a response to a nearly 6 year old question. What I cannot figure out is if you are trying to offer a solution with the code you posted or if you have a question related to the topic of this thread and forgot to include a worded description of your question.
My response in an answer (to a six year old) question: how to initialize a multidimensional array at once, with a data set used in the original question.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top