Re: Handling ubound on an uninitialised array
From: Tushar Mehta (tmUnderscore200310_at_tushar-mehta.SeeOhEm)
Date: 01/24/05
- Next message: gocush: "Date format - ADO query Access to Excel"
- Previous message: Jim May: "Re: Complie Error Help"
- In reply to: keepITcool: "Re: Handling ubound on an uninitialised array"
- Next in thread: Mike NG: "Re: Handling ubound on an uninitialised array"
- Reply: Mike NG: "Re: Handling ubound on an uninitialised array"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 24 Jan 2005 04:46:27 GMT
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 <xn0dxmi6t9pzc4500xkeepitcoolnl@msnews.microsoft.com>, xrrcvgpbby@puryyb.ay 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. >
- Next message: gocush: "Date format - ADO query Access to Excel"
- Previous message: Jim May: "Re: Complie Error Help"
- In reply to: keepITcool: "Re: Handling ubound on an uninitialised array"
- Next in thread: Mike NG: "Re: Handling ubound on an uninitialised array"
- Reply: Mike NG: "Re: Handling ubound on an uninitialised array"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|