Excel VBA - Create a jagged two dimensional array

Harry Flashman

Active Member
Joined
May 1, 2011
Messages
361
I can create a two dimensional array, but in this case I think I really need a jagged two dimensional array.

Cell A1 contains the following:
[TABLE="width: 766"]
<tbody>[TR]
[TD="width: 766"]A,1,2,3;B,1,2,3,4,5,6;C,1,2[/TD]
[/TR]
</tbody>[/TABLE]
The semicolons split the data into rows, and the commas split the data in to columns.

I can take this value and put it into a two dimensional array with the following routine:

Code:
Sub PutCellContentsIntoTwoDimensionaArray()

    Dim rng As Range
    Set rng = Range("A1")
    
    Dim arrRow As Variant
    arrRow = Split(rng, ";")
    
    Dim i As Integer, j As Integer, cnt As Integer
    For i = LBound(arrRow) To UBound(arrRow)
        
        cnt = UBound(Split(arrRow(i), ","))
        If cnt > j Then
            j = cnt
        End If
        
    Next i
    
    Dim r As Integer, c As Integer
    Dim arrCol As Variant, arr() As Variant
    
    For r = LBound(arrRow) To UBound(arrRow)
        arrCol = Split(arrRow(r), ",")
        For c = LBound(arrCol) To UBound(arrCol)
            ReDim Preserve arr(0 To i, 0 To j)
            'Debug.Print c & " - " & arrCol(c)
            arr(r, c) = arrCol(c)
        Next c
    Next r
    
    ' print the first value in each row
    Debug.Print arr(0, 0)
    Debug.Print arr(1, 0)
    Debug.Print arr(2, 0)

End Sub

The trouble is it is a ordinary rectangular two dimensional array. Is there some way I can modify this code to create a two dimensional jagged array instead (assuming this is possible).

My routine can print the first value in each row of the array, but I am not sure how to print the last value in each row of the array, as the number of columns containing values changes with each row.

Any advice would be greatly appreciated. Cheers
 
Last edited:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Last edited:
Upvote 0
I have added comments to the routine to make it a bit easier to understand.

Code:
Sub GetValuesAndPutIntoTwoDimensionaArray()

    Dim rng As Range
    Set rng = Range("A1")
    
    Dim arrRow As Variant
    arrRow = Split(rng, ";")
    
    ' figure out how many columsn are in the two-dim array
    Dim i As Integer, j As Integer, cnt As Integer
    For i = LBound(arrRow) To UBound(arrRow)
        
        cnt = UBound(Split(arrRow(i), ","))
        If cnt > j Then
            j = cnt
        End If
        
    Next i
    
    i = i - 1
    
    ' i + 1 is the number of rows
    ' j + 1 is the number of columns
    
    Dim r As Integer, c As Integer
    Dim arrCol As Variant, arr() As Variant
    
    ' loop through each row of the array
    For r = LBound(arrRow) To UBound(arrRow)
    ' and split the comma separated values from each row into separate columns
        arrCol = Split(arrRow(r), ",")
        For c = LBound(arrCol) To UBound(arrCol)
            ReDim Preserve arr(0 To i, 0 To j)
            'Debug.Print c & " - " & arrCol(c)
            ' populate our two-dim array
            arr(r, c) = arrCol(c)
        Next c
    Next r
    
    ' print the column headers
    Debug.Print arr(0, 0)
    Debug.Print arr(1, 0)
    Debug.Print arr(2, 0)

End Sub
 
Upvote 0
If you don't mind having the arrays be zero-based (that is, the first element for each dimension is 0, not 1) and if you don't mind using an odd syntax to address the array elements, then give this code a try...

Code:
Sub CreateJaggedArray()

  Dim R As Long, C As Long, Jag() As String, Jagged() As Variant
  
  [COLOR="#008000"]' Create the jagged array from the text in cell A1.[/COLOR]
  Jag = Split(Range("A1").Value, ";")
  ReDim Jagged(0 To UBound(Jag))
  For R = 0 To UBound(Jag)
    Jagged(R) = Split(Jag(R), ",")
  Next
  
  [COLOR="#008000"]' Show the data stored in the jagged array starting in cell C3.[/COLOR]
  [COLOR="#008000"]' Note the "odd" syntax for addressing the array's two dimensions.[/COLOR]
  For R = 0 To UBound(Jagged)
    For C = 0 To UBound(Jagged(R))
      Range("C3").Offset(R, C).Value = Jagged(R)(C)
    Next
  Next
  
End Sub

The elements of the array are specified by enclosing each dimension in its own set of parentheses and not as a comma delimited list.
 
Upvote 0
That is brilliant thanks Rick. One more question if you don't mind.

How can I find out the number of elements in each row of the jagged array?

For example the row starting with A has three additional elements, while row B has six additional elements. How can I return the number of elements for a specified row of this jagged array?
 
Upvote 0
Sorry that was a silly question. The answer is right there in your code.
If I want to know the number of elements in the second row....
x = UBound(Jagged(1))

Thanks once again Rick. :)
 
Upvote 0
How can I find out the number of elements in each row of the jagged array?
Because the arrays are zero-based, the number of elements in any give row of the jagged array is one plus the UBound of that row's array. See if this example subroutine makes this clear (the print out will be in the Immediate Window)...
Code:
Sub CreateJaggedArray()

  Dim R As Long, C As Long, Jag() As String, Jagged() As Variant
  
  [COLOR="#008000"]' Create the jagged array from the text in cell A1.[/COLOR]
  Jag = Split(Range("A1").Value, ";")
  ReDim Jagged(0 To UBound(Jag))
  For R = 0 To UBound(Jag)
    Jagged(R) = Split(Jag(R), ",")
  Next
  
  [COLOR="#008000"]' Display the element count per array.[/COLOR]
  For R = 0 To UBound(Jagged)
    Debug.Print "There are " & [COLOR="#0000FF"]UBound(Jagged(R)) + 1[/COLOR] & _
                " elements on Row " & R & " of the jagged array."
  Next
  
End Sub
 
Last edited:
Upvote 0
Sorry that was a silly question. The answer is right there in your code.
If I want to know the number of elements in the second row....
x = UBound(Jagged(1))
Correct... the thing to keep in mind here is that you have an array (that is what the first number in parentheses addresses) each of whose elements are an array (which is what the second number in parentheses addresses)... and, of course, you have to remember that all arrays are zero based so that you end up referencing the correct array elements (such as your use of 1 to address the second row).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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