Re: Handling ubound on an uninitialised array

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Tushar Mehta (tmUnderscore200310_at_tushar-mehta.SeeOhEm)
Date: 01/24/05


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.
> 


Relevant Pages

  • Re: Max/Min Functions
    ... You said the Array function won't work here. ... Dim myArray() As Variant ... > Dim vMax As Variant ...
    (microsoft.public.word.vba.general)
  • Re: Max/Min Functions
    ... You can stuff a value of any other data type into a Variant ... > Dim myArrayAs Variant ... > a whole array into a single Variant variable, ... > Dim vMax As Variant ...
    (microsoft.public.word.vba.general)
  • Re: Same Contents, Regardless of Order
    ... Dim Arr1As Variant ... ' Second Array ... Dim blnFound As Boolean ...
    (microsoft.public.excel.programming)
  • Re: transferring array to range
    ... Dim arrValues(NumRows, NumColumns) As Variant ... declared an array of variants instead of a variant that contains an array. ...
    (microsoft.public.excel.programming)
  • Re: Re:workdays
    ... Optional Holidays As Variant = Nothing, ... Dim arrayH As Variant ... ' or not an array or cell range with at least one numeric value between ... ReDim arrayH) As Variant ...
    (microsoft.public.excel)