0

A question on VBA

If i declare an array variable like

Dim myArray() as string

How can i populate the array as two-dimensional when the

myArray = Array(arglist)

takes only an arglist that is comma-delimited and the arguments represent one dimension?

thanks

2 Answers 2

3

You could use following code:

Dim myArray As Variant

myArray = Array(Array("a", "b"), Array("c", "d"))

Note, that since Array returns Variant (link), you should declare your variable myArray as Variant

Sign up to request clarification or add additional context in comments.

4 Comments

Yes but Array can take string data to. Dim myArray() as String myArray = Array("t1","t2") Could you be kind enough to explain me cause your answer bore me a question :P
Yes, Array can take String data, but not only String. It can take any other data type. You could write e.g. myArray = Array("t1",123) and array will contain both String and Integer. And since array can contain any data, it returns Variant - a special data type that can contain any kind of data (see link here). And since Array returns Variant, you have to declare Dim myArray As Variant
You the man my friend!!!! Thanks a billion if i can push something to give you reputation please tell :P
it's all ok! You've already given me reputation accepting answer:)
1

You could define your array differently than using the Array() function. Working from this from Microsoft, you'd do the following.

Dim myArray(5,5) as String
'If you wanted to loop this for example, you could use UBound, as seen in this
'http://msdn.microsoft.com/en-us/library/95b8f22f%28v=vs.90%29.aspx
Dim x as Integer, y as Integer
For x = 1 to UBound(myArray,1)
     For y = 1 To UBound(myArray,2)
          myArray(x,y) = "foo" + CStr(x) + CStr(y)
     Next y
Next x

Otherwise you could define it normally with something like simoco mentioned

Dim myArray(2,2) as String
myArray(1,1) = "a"
myArray(1,2) = "b"
myArray(2,1) = "c"
myArray(2,2) = "d"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.