The foll. works just fine for me. The dimension of any variable that is
not usable as an array is 0.
Option Explicit
Function NbrDimensions(x As Variant) As Byte
Dim i As Byte, n As Long
i = 1
On Error GoTo ErrXIT
Do While True
n = LBound(x, i)
i = i + 1
Loop
ErrXIT:
NbrDimensions = i - 1
End Function
Sub testIt()
Dim i As Integer, s As String, r As Range, _
x(1 To 10) As Long, y, z()
MsgBox NbrDimensions(i) & "," & NbrDimensions(s) & "," _
& NbrDimensions(r) & "," _
& NbrDimensions(x) & "," & NbrDimensions(y) & "," _
& NbrDimensions(z)
Set r = Range("a1:B10")
ReDim y(1 To 10, 1 To 10)
ReDim z(-10 To -1, 0 To 0, 100 To 102)
MsgBox NbrDimensions(i) & "," & NbrDimensions(s) & "," _
& NbrDimensions(r) & "," _
& NbrDimensions(x) & "," & NbrDimensions(y) & "," _
& NbrDimensions(z)
End Sub
If you must distinguish between an unitialized dynamic array and a non-
array, use
Function NbrDimensions(x As Variant) As Integer
Dim i As Byte, n As Long
If Not IsArray(x) Or TypeName(x) = "Range" Then _
NbrDimensions = -1: Exit Function
i = 1
On Error GoTo ErrXIT
Do While True
n = LBound(x, i)
i = i + 1
Loop
ErrXIT:
NbrDimensions = i - 1
End Function
--
Regards,
Tushar Mehta
www.tushar-mehta.com
Multi-disciplinary business expertise
+ Technology skills
= Optimal solution to your business problem
Recipient Microsoft MVP award 2000-2005