Reputation: 1875
Normally when looking for amount of diensions, or their bounds you can user UBound, and LBound.
I am stuck right now because I have a dynamic array and I want to check if it had its first ReDim ( or if it has at least one dimension).
Without as single dimension UBound( array, 1) is out of range.
So how do I check if it has zero dimesions?
Result shoud look more or less like this:
Dim array()
If NO_DIMENSIONS Then
...something...
End if
Upvotes: 1
Views: 59
Reputation: 4296
You can use error trapping to accomplish this.
Function NumberOfDimensions(arr As Variant)
'Sets up the error handler.
On Error GoTo FinalDimension
'Visual Basic for Applications arrays can have up to 60000
'dimensions; this allows for that.
For DimNum = 1 To 60000
'It is necessary to do something with the LBound to force it
'to generate an error.
ErrorCheck = LBound(arr, DimNum)
Next DimNum
FinalDimension:
NumberOfDimensions = DimNum - 1
End Function
This code was modified into a function from code taken from Microsoft support.
So you would use as such:
Dim testArr()
If NumberOfDimensions(testArr) = 0 Then
'Do Something
End If
In this method, you are taking that subscript out of range error and using it to count how many dimensions there are.
Upvotes: 2