0

the idea here is that I have a table with column a wanting it to be the first part of the array and column B which is an integer, wanting it to be the length of the second part of the array.

The question here is that is it possible for an array to have different lengths for the upperbounds of its 2nd part?

This is how the table looks like

and this is my code, but it doesnt seem to work

Dim rowssheet2 As Variant
rowssheet2 = ThisWorkbook.Worksheets("Sheet2").Range("b2").End(xlDown).Row - 1
Dim arr2()
 ReDim arr2(rowssheet2)

 For i = 0 To rowssheet2
  ReDim arr2(i, ThisWorkbook.Worksheets("Sheet2").Range("B" & 2 + i).Value)
Arr2(i, 0) = ThisWorkbook.Worksheets("Sheet2").Range("A" & 2 + i).Value

Next i

Going forward if you can answer that and if this is possible, then i am trying to iterate through the array, but the Ubound(arr2,2) is not going to be the same for all of the array

Community
  • 1
  • 1
Tony
  • 5
  • 3
  • 1
    No, not the way you're doing it. The array will always be rectangular, not 'ragged edge'. –  Jun 26 '18 at 14:41

1 Answers1

1

Strictly speaking, there is no way for a VBA array to have different bounds in the second dimension depending on the index of the first one. This is rather apparent from the implementation of arrays in VBA. (Actually, the source talks about VB6 but there is no difference in VBA.)

What you are doing in the loop is redimming both dimensions for the entire array. (See section 5.4.3.3 of the VBA specification.)

However, the concept you try to realize, a jagged array, i.e. an array of arrays, can be build in VBA. See this SO answer for how to do it.

M.Doerner
  • 712
  • 3
  • 7