Multidimensional array in VBA

Stevenn

Active Member
Joined
Feb 8, 2012
Messages
259
How can I make a multidimensional array like this:

[ [ "Name 1", [ "A", "B", "C" ] ], [ "Name 2", [ "D", "E", "F", "G", "H" ] ], [ "Name 3", [ "I" ] ] ]

It's a total of three groups which each has a name. I don't know how to create and populate such arrays in VBA and how to finally print it.
 
Hi

There are several options.

You can use your information about the structure of the jagged array, like
- an undefined number of rows but always 2 columns
- the first element in each row is a string, the second an array with an undefined number of elements

This allows you to make a smart print, like:

Code:
Option Explicit
Option Base 1

Public Sub Test1()
Dim i As Long, k As Long
Dim vArr(1 To 3, 1 To 2) As Variant


    vArr(1, 1) = "Group 1"
    vArr(1, 2) = Array("1234", "2345", "3456")


    vArr(2, 1) = "Group 2"
    vArr(2, 2) = Array("4567", "8765", "3214", "4123", "5773")


    vArr(3, 1) = "Group 3"
    vArr(3, 2) = Array("8884")
    
    For i = 1 To UBound(vArr)
 
       Debug.Print vArr(i, 1)
        
        For k = 1 To UBound(vArr(i, 2))
            Debug.Print vArr(i, 2)(k)
        Next k
 
   Next i

End Sub


Does this solve your problem?


Thats the one I thought of atleast, the UBound function for arrays =)
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi

There are several options.

You can use your information about the structure of the jagged array, like
- an undefined number of rows but always 2 columns
- the first element in each row is a string, the second an array with an undefined number of elements

This allows you to make a smart print, like:

Code:
Option Explicit
Option Base 1

Public Sub Test1()
Dim i As Long, k As Long
Dim vArr(1 To 3, 1 To 2) As Variant


    vArr(1, 1) = "Group 1"
    vArr(1, 2) = Array("1234", "2345", "3456")


    vArr(2, 1) = "Group 2"
    vArr(2, 2) = Array("4567", "8765", "3214", "4123", "5773")


    vArr(3, 1) = "Group 3"
    vArr(3, 2) = Array("8884")
    
    For i = 1 To UBound(vArr)
 
       Debug.Print vArr(i, 1)
        
        For k = 1 To UBound(vArr(i, 2))
            Debug.Print vArr(i, 2)(k)
        Next k
 
   Next i

End Sub


Does this solve your problem?

It does solve my problem :) Thank you! Great help!
 
Upvote 0

Forum statistics

Threads
1,216,094
Messages
6,128,785
Members
449,468
Latest member
AGreen17

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