INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Test for Empty String() Array

Test for Empty String() Array

(OP)
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

RE: Test for Empty String() Array

2
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

RE: Test for Empty String() Array

(OP)
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

RE: Test for Empty String() Array

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

RE: Test for Empty String() Array

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

RE: Test for Empty String() Array

Dim wombat() As Long   
MsgBox IsArray(wombat)
MsgBox UBound(wombat)

RE: Test for Empty String() Array

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

RE: Test for Empty String() Array

Ah, a precious moment in Tek-Tips history.  Having a snooze are we?

Gerry

RE: Test for Empty String() Array

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

RE: Test for Empty String() Array

That version suffers a problem if you do have an initialised  array, but it isn't a string array (or variant array of strings) ...

RE: Test for Empty String() Array

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

RE: Test for Empty String() Array

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

RE: Test for Empty String() Array

Yep. Fair point. Original question certainly was. I thought we'd moved on to generic solutions, though, as the thread developed.

RE: Test for Empty String() Array

(and it won't discriminate between the subtly different dim wombat() and dim wombat(0) )

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close