Dr. Demento
Well-known Member
- Joined
- Nov 2, 2010
- Messages
- 618
- Office Version
- 2019
- 2016
- Platform
- 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.
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