Test for end of array of objects?

P

peter

If I'm looping thru an array of objects with 'For Each',
how do I test for the last good one?

If I test for "" or 0, it says "not supported"

If I test for Nothing, it says "invalid use of object"

Thanks,
Peter.

(Excel VBA under XP)


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
T

Tim Williams

Whatever you're iterating through probably has a "count" property, so
you could try using a counter and checking to see if you're on the
last item.

Or just keep a reference to the last object and use it once you're out
of the loop.

Tim.
 
T

Tim Williams

Then you could iterate the objects into a variant array and loop
through that instead (checking the ubound)

Tim
 
P

peter

I think the problem is doing the test of the Variant, whereever it comes
from.

Why do I have to use a Variant anyway? Shouldn't it understand if I use
the class of the contained objects?




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
B

Bob Phillips

Alternative approach. Create a collection class for the objects and build a
Count property.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
T

Tom Ogilvy

Sub Tester1()
Dim MyArray() As Object

ReDim MyArray(0 To 3)
Set MyArray(0) = CommandBars(1)
Set MyArray(1) = Range("A1")


i = LBound(MyArray) - 1
For Each element In MyArray

i = i + 1
If element Is Nothing Then
MsgBox i & " is the first empty element"
Exit For
End If
Next

End Sub

--
Regards,
Tom Ogilvy

Colo said:
peter,
In a loop, you may use TypeName(element)??

A sample in use.
http://puremis.net/excel/code/076.shtml
Why do I have to use a Variant anyway?
Please have a look at the For Each...Next Statement in the VBA help.
For arrays, element can only be a Variant variable.
[unquote]


--
Regards,
Colo
http://www.puremis.net/excel/


peter said:
I think the problem is doing the test of the Variant, whereever it comes
from.

Why do I have to use a Variant anyway? Shouldn't it understand if I use
the class of the contained objects?




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top