Jagged Arrays Problem - How to Assign Arrays to an Array

Z

Zigs

I am trying to assign an array to an array to create a jagged array. I
am not sure if this is possible in VBA because when i searched online
the examples were given in VB.Net, but here is the code that I am
using to do this:

Dim vArr1(1 to 10) as Variant
Dim vArr2(1 to 10) as Variant
Dim vMainArr(1 to 2) as Variant
Dim lCounter as Long

'Populate the sub arrays:
For lCounter = 1 to 10
vArr1(lCounter) = lCounter
vArr2(lCounter) = lCounter
Next lCounter

'Assign the sub arrays to the main array:
vMainArr(1) = vArr1
vMainArr(2) = vArr2

However, an error is thrown at the two lines of code above.
 
B

Bob Phillips

See other post.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
T

Tom Ogilvy

Sub ABC()
Dim vArr1(1 To 10) As Variant
Dim vArr2(1 To 5) As Variant
Dim vMainArr(1 To 2) As Variant
Dim lCounter As Long
Dim s as String, i as Long, j as Long

'Populate the sub arrays:
For lCounter = 1 To 10
vArr1(lCounter) = lCounter
if lCounter < 6 then _
vArr2(lCounter) = lCounter * 2
Next lCounter

'Assign the sub arrays to the main array:
vMainArr(1) = vArr1
vMainArr(2) = vArr2
s = ""
For i = 1 To 2
For j = LBound(vMainArr(i), 1) To UBound(vMainArr(i), 1)
s = s & vMainArr(i)(j) & ","
Next j
s = s & vbNewLine
Next i
MsgBox s
End Sub

works fine for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top