Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the No. of dimensions of an array?

Status
Not open for further replies.

lindseyp

Technical User
Apr 25, 2002
1
US
I'm using Ubound to find the size of an array that I'm passed in a function, but I can't figure out how to determine the number of dimensions.

AFAIK...
1 Vartype returns the value vbArray to tell me it's an array, but no extra info to tell me the number of dimensions. For a 2 dimensional array, VARTYPE(myarray) < 2 * vbArray

2 myarray(1) is an integer for a 1 dimensional array, but it is not a valid call when there are more than 1 dimension. i was trying to check whether vartype(myarray(1)) was vbArray, but it crashes when there's more than 1 dimension.


Can anyone help with this? I can't believe there isn't a function which returns the number of dimensions.
 
I have not done this..BUT
Dim lngDims
Dim lngTest
For I = 2 to 99
On error resume Next
lngTest = UBound(ary,I)
if err.Number <> 0 then lntTest = -1
On error goto 0
if lngTest < 0 then exit for
Next
lngDims = I - 1
Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Expounding upon JohnYingling's idea, here is a complete function and test sub code. Drop this into a code module. Test the function by declaring different dimensions for myArray (currently 3 dimensions) . I have tested this up to 60 dimensions (61 dimensions fails to compile, as advertised).


Code:
Option Explicit
Option Base 1

Dim myArray(10, 10, 10) As Integer


Sub Test()
Dim NumOfDims As Integer

Debug.Print GetArrayDimensions(myArray)

End Sub

Function GetArrayDimensions(ByRef Arr As Variant) As Integer
Dim x As Long
Dim i As Integer

On Error Resume Next
For i = 1 To 60  'VBA Documentation says 60 is max dimensions
  x = UBound(Arr, i)
  If Err.Number = 9 Then Exit For
Next i
GetArrayDimensions = i - 1
Next i

End Function


Regards,
M. Smith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top