Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test for Empty String() Array 3

Status
Not open for further replies.

AccordingToDale

Programmer
Jul 11, 2005
128
CA
Any idea how to test if an Array has any elements? (Other than trapping the error.)

In other words, if you try to get a value from an Array (or for that matter, try to get the UBounds) that has no elements, you get an "Subscript Out of Range" error. How do you avoid raising that error?

This is in VBA.

D
 
And what about the IsArray function ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Unfortunately this only tests if the variable is an array, but not if the array is empty.

What I think I will end up with is:

In some function I'll call an validation function
Code:
    If validateArray(Attachment) Then
        'do something
    end if

then...
Code:
Private Function validateArray(arr As Variant) As Boolean
    On Error GoTo catch 'if not initialised
    Dim i As Long   'to test array
    If IsArray(arr) Then
        'UBound will fail if the array is uninitialised
        i = UBound(arr) 
        validateArray = True 'else it's cool
    End If
    Exit Function

catch:
    validateArray = False
End Function

Thanx. anyway.

D
 
Hi D,

Since arrays for which dimensions are set using the To clause in a Dim, Private, Public, ReDim, or Static statement can have any integer value as a lower bound, testing the UBound value alone could be misleading. In that case, you might find that the following gives a more reliable result:
Code:
Private Function validateArray(arr As Variant) As Boolean
    On Error Resume Next
    If LBound(arr) = UBound(arr) Then
        validateArray = False
        Exit Function
    Else
        validateArray = True
    End If
End Function

Cheers
 
I'm curious. Under what circumstances does the IsArray function, all by itself, not give the right result? When will it return True followed by UBound giving an error?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Dim wombat() As Long
MsgBox IsArray(wombat)
MsgBox UBound(wombat)
 
Alright, I'll wake up shortly :)

Thanks Mike

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Ah, a precious moment in Tek-Tips history. Having a snooze are we?

Gerry
 
If Len(Join(Attachment)) > 0 Then
' Do stuff with initialized array

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
That version suffers a problem if you do have an initialised array, but it isn't a string array (or variant array of strings) ...
 
zzzzzzzzzzzzzzzzzzzzzzzzz

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
strongm, the thread's subject is about String() array.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Yep. Fair point. Original question certainly was. I thought we'd moved on to generic solutions, though, as the thread developed.
 
(and it won't discriminate between the subtly different dim wombat() and dim wombat(0) ;-))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top