View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rob van Gelder[_4_] Rob van Gelder[_4_] is offline
external usenet poster
 
Posts: 1,236
Default Handling ubound on an uninitialised array

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.

--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Tushar Mehta" wrote in message
...
While this would work for 99.999% of the cases, it would report an
erroneous result if the upper bound were -1! {g}

Sub testIt()
Dim x(), UB As Long
On Error Resume Next
UB = UBound(x)
MsgBox IIf(Err.Number < 0, "X is empty", "X ubound=" & UB)
On Error GoTo 0
ReDim x(-10 To -1)
On Error Resume Next
UB = UBound(x)
MsgBox IIf(Err.Number < 0, "X is empty", "X ubound=" & UB)
On Error GoTo 0
End Sub

--
Regards,

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

In article ,
says...
I do the On Error Resume Next thing to determine the length of an
array...
I've seen others do it to.

To wipe an array use the Erase statement.

Sub test()
Dim arr() As String, lng As Long

' ReDim arr(0)

lng = -1
On Error Resume Next
lng = UBound(arr)
On Error GoTo 0

MsgBox "array length is " & IIf(lng = -1, "empty", lng)

Erase arr
End Sub

Just a note. If I were you, I would start working in Option Base 0. It is
more compatible with other programming languages.