09-15-2025, 11:54 PM
Something like this might be a little better for this type routine:
Code: (Select All)
Dim a(2, 2, 2) As Integer ' Example: a 3-dimensional array
Print "Number of dimensions: "; CountDimensions(a())
Sleep
Print UBound(a, 6) 'to show that error reports now work as expected
End
ErrorHandler:
If Err = 9 Then ' Subscript out of range error
exitcount = _TRUE
Resume Next ' Continue execution after the error
Else
Print "An unexpected error occurred: "; Err
End
End If
Function CountDimensions (arr() As Integer)
On Error GoTo ErrorHandler
Shared exitcount
exitcount = _FALSE
Do
numDimensions = numDimensions + 1
' Attempt to get the upper bound of the current dimension
' If this dimension doesn't exist, an error will occur
dummy = UBound(arr, numDimensions)
Loop Until exitcount
CountDimensions = numDimensions - 1 'subtract 1 as the last 1 was the error
On Error GoTo 0 'turn off error handling so we get proper error reports
End Function

