Im trying to Check a string against an array.Its Basicaly
If the string is in the array then stop .
So I Loop through the array and compare , thats fine .
But When the array is empty I get a script out of bounds error . Is there a way around this ?
Printable View
Im trying to Check a string against an array.Its Basicaly
If the string is in the array then stop .
So I Loop through the array and compare , thats fine .
But When the array is empty I get a script out of bounds error . Is there a way around this ?
try adding
at the beginning of the sub.Code:On Error Resume Next
I am sure there is a way so you dont get any error, but this is the lazy way out.
Start your loop with
or end it withCode:Do Until X>Ubound(array)
or have a for next loopCode:Loop until X>=ubound(array)
Code:For X=lbound(array) to ubound(array)
I recomend the For...Next Loop, those are my favorite :D
I guess you could have a Goto
But i don't recommend it, For Next is my favourite but Do, or While Loop are useful tooCode:1:
X=X+1
If ubound(array)>=x then goto 1
The Goto command is NEVER recommended, except when it's completely impossible to not use it. :rolleyes:
There is only one such case:
On Error Goto Oops
The Problem lies right when I callQuote:
Originally posted by kedaman
Start your loop with
or end it withCode:Do Until X>Ubound(array)
or have a for next loopCode:Loop until X>=ubound(array)
Code:For X=lbound(array) to ubound(array)
this = ubound(usr)
due to the fact that usr() has no Ubound if its empty
Code:Private Sub ParseArray()
On Error GoTo ArrayErr
this = UBound(usr)
'do whatever
Exit Sub
ArrayErr:
If Err.Number = 9 Then
Call MsgBox("Empty array", vbInformation)
Else
'oops
End If
End Sub
Here's my not so(yet) famous fake ubound function, actually it's the get part of a property
It will return the upperbound if intialized, or -1 if not. Therefore it wont go trough the loop at allCode:Property Get UUbound(Uarray)
UUbound = -1
On Error Resume Next
UUbound = UBound(Uarray)
End Property
[code]
For X=0 to uubound(array)
next x
[code]