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:
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
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: