Determine bounds of jagged array

Dr. Demento

Well-known Member
Joined
Nov 2, 2010
Messages
618
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
From John Coleman on Stack Overflow, I pieced together the function below. However, I'm now dealing with arrays of arrays or jagged arrays. I'm curious how I can adapt this function to detect and report the number of dimensions for each array within a single array.

Given my example the the test_arr_Bounds, I would expect for 'arr' the answer would be

Total overall # of dimensions: ???
arr = 0 to 3 | one dimension
arr(0) =0 to 7 | one dimension
arr(1) = 0 to 6 | one dimension
arr(2) = 0 to 5 | one dimension
arr(3) = 1 to 2, 1 to 3 | two dimensions

To be honest, I'm not even sure how many dimensions combining the count of all arrays within 'arr' that would come to. One for arr, one for arr(0) to arr(2) and two for arr(3); so 6 maybe?? I'm really not sure.

If the existing function is inadequate, I'm more than happy to adapt. John mentions that Ubound is fragile and requires error trapping, which is why he went with the collection route, I think.

Any help would be most appreciated.

Thanks y'all.

VBA Code:
Function arr_Bounds(rra As Variant, _
                    Optional nlbOutput As Boolean = False) As Collection
' ~~ Find bounds of an array
' http://stackoverflow.com/a/35671729

Dim coll As New Collection
Dim arr As Variant
Dim cntr As Long, _
    i As Long, _
    k As Long
  cntr = 1
Dim str As String

  On Error GoTo exit_function

  Do While True
    arr = Array(LBound(rra, cntr), UBound(rra, cntr))
    coll.Add arr
    cntr = cntr + 1
  Loop
 
exit_function:

  Set arr_Bounds = coll
 
  If nlbOutput = True Then
 
    str = "Array has [" & coll.Count & "] dimensions: "
    
    For i = 1 To coll.Count
      k = k + (coll(i)(1) - coll(i)(0) + 1)
      str = str & "[" & i & "] (" & coll(i)(0) & ", " & coll(i)(1) & ") || " '& " | " & k
    Next i

    If right(str, 4) = " || " Then _
      str = StrReverse(Replace(StrReverse(str), (" || "), vbNullString, 1, 1))
    Debug.Print str

  End If 'nlbOutput
 
End Function

Sub test_arr_Bounds()

' ~~ Simple testing
Dim a(1 To 10, 1 To 5, 4 To 10) As Integer
Dim b(1 To 5) As Variant
Dim c As Variant

  arr_Bounds a, True
  arr_Bounds b, True
  arr_Bounds c, True
  
' ~~ Test for JAGGED ARRAY
Dim arr As Variant
  ReDim arr(0 To 3)

  arr(0) = Array("THIS", "IS", vbNullString, vbNewLine, "A", 0, "TEST")
  arr(1) = Array("I", "can't", "believe", "it's", "not", "butter")
  arr(2) = Split("You talkin' to me, Willis?", Space(1))
  arr(3) = [a1:b3]

  arr_Bounds arr, True
  
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,215,516
Messages
6,125,280
Members
449,220
Latest member
Excel Master

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