Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
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 now!
  • 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.

AccordingToDale (Programmer) (OP)
14 Oct 05 13:43
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
Helpful Member!(2)  PHV (MIS)
14 Oct 05 14:41
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

AccordingToDale (Programmer) (OP)
14 Oct 05 14:58
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
Helpful Member!  macropod (TechnicalUser)
16 Oct 05 22:58
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
TonyJollans (Programmer)
17 Oct 05 10:08
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

strongm (MIS)
17 Oct 05 14:59
Dim wombat() As Long   
MsgBox IsArray(wombat)
MsgBox UBound(wombat)
TonyJollans (Programmer)
17 Oct 05 15:07
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

fumei (TechnicalUser)
17 Oct 05 17:29
Ah, a precious moment in Tek-Tips history.  Having a snooze are we?

Gerry

PHV (MIS)
17 Oct 05 17:34
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

strongm (MIS)
17 Oct 05 17:42
That version suffers a problem if you do have an initialised  array, but it isn't a string array (or variant array of strings) ...
TonyJollans (Programmer)
17 Oct 05 17:50
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

PHV (MIS)
17 Oct 05 17:59
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

strongm (MIS)
17 Oct 05 18:09
Yep. Fair point. Original question certainly was. I thought we'd moved on to generic solutions, though, as the thread developed.
strongm (MIS)
17 Oct 05 18:15
(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!

Back To Forum

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