View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
Tushar Mehta Tushar Mehta is offline
external usenet poster
 
Posts: 1,071
Default Handling ubound on an uninitialised array

The simplest function that I can think of for finding the number of
dimensions:

Function ArrDim(vArr As Variant) As Integer
Dim i As Long, x As Long
On Error GoTo XIT
i = 1
Do
x = LBound(vArr, i)
i = i + 1
Loop While True
XIT:
ArrDim = i - 1
End Function

Of course, it doesn't distinguish between an uninitialized array and a
non-array, returning zero in both cases. To distinguish between the
two:
Function ArrDim(vArr As Variant) As Integer
Dim i As Long, x As Long
If Not IsArray(vArr) Then ArrDim = -1: Exit Function '<<<<<
On Error GoTo XIT
i = 1
Do
x = LBound(vArr, i)
i = i + 1
Loop While True
XIT:
ArrDim = i - 1
End Function


--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article om,
says...
Rob,

I agree with you that in many cases it's preferable to work with 0
based arrays (that is with a LOWERbound of 0)

However Excel will return 1 based arrays on many of it's objects
properties

I believe that Tushar's comment re an UPPERBOUND of -1 may relate
to some functions like split/filter or a scripting dictionary's items
array which return an (0 to -1) array if no results were found.

I've just written following function which gives the DIMENSIONS of an
array. -1 for NO array, 0 for uninitialized etc. According to VBA help
vb can handle a max of 60 dimensions. (if you dont run out of memory)

Function ArrDim(vArr As Variant) As Integer
Dim i%
On Error Resume Next
If IsArray(vArr) Then
For i = 0 To 59
If IsError(LBound(vArr, i + 1)) Then Exit For
Next
Else
i = -1
End If
ArrDim = i
End Function

Once you know the dimensions you can safely test the lbound and ubound.


HTH




--
keepITcool
|
www.XLsupport.com | keepITcool chello nl | amsterdam


Rob van Gelder wrote :

I was wondering if anyone would point that out...

In my view it's wrong to set lbound to anything other than zero - I
hinted at that with my Base 0 comment.