PC Review


Reply
Thread Tools Rate Thread

How to track object index in object collection

 
 
ExcelMonkey
Guest
Posts: n/a
 
      9th Jun 2008
How do you return the object index within a collection. If I have a
collection of ranges and I want to loop thru the collection using a For Each
loop. What property do I track for each rng to obtain the collection number?
Or do I have to use a Counter variable?

Set colRanges = New Collection

For Each rng2 In colRanges

Next

Thanks
EM


 
Reply With Quote
 
 
 
 
Jim Thomlinson
Guest
Posts: n/a
 
      9th Jun 2008
Perhaps this...

Not sure if this is what you want...

Sub test()
Dim colRanges As Collection
Dim rng As Range
Dim lng As Long

Set colRanges = New Collection
With colRanges
.Add Range("A1:A10")
.Add Range("B1:B10")
.Add Range("C1:C10")
End With

For Each rng In colRanges
MsgBox rng.Address
Next rng

For lng = 1 To colRanges.Count
MsgBox colRanges(lng).Address
Next lng

End Sub
--
HTH...

Jim Thomlinson


"ExcelMonkey" wrote:

> How do you return the object index within a collection. If I have a
> collection of ranges and I want to loop thru the collection using a For Each
> loop. What property do I track for each rng to obtain the collection number?
> Or do I have to use a Counter variable?
>
> Set colRanges = New Collection
>
> For Each rng2 In colRanges
>
> Next
>
> Thanks
> EM
>
>

 
Reply With Quote
 
 
 
 
JE McGimpsey
Guest
Posts: n/a
 
      9th Jun 2008
One way:

Dim colRanges As Collection
Dim i As Long

Set colRanges = New Collection
colRanges.Add Range("A1:A10")
colRanges.Add Range("B1:B10")
colRanges.Add Range("C1:C10")
For i = 1 To colRanges.Count
If colRanges.Item(i).Address(False, False) = "B1:B10" Then _
MsgBox i
Next i


In article <69F81935-1E10-4E77-BE01-(E-Mail Removed)>,
ExcelMonkey <(E-Mail Removed)> wrote:

> How do you return the object index within a collection. If I have a
> collection of ranges and I want to loop thru the collection using a For Each
> loop. What property do I track for each rng to obtain the collection number?
> Or do I have to use a Counter variable?
>
> Set colRanges = New Collection
>
> For Each rng2 In colRanges
>
> Next
>
> Thanks
> EM

 
Reply With Quote
 
Jim Thomlinson
Guest
Posts: n/a
 
      9th Jun 2008
Just curious. Is there any advantage to using

colRanges.Item(i)
over
colRanges(i)

I use the .item when I have the object as part of a with but otherwise I
tend to omit it.

with colRanges
.item(i)
end with

--
HTH...

Jim Thomlinson


"JE McGimpsey" wrote:

> One way:
>
> Dim colRanges As Collection
> Dim i As Long
>
> Set colRanges = New Collection
> colRanges.Add Range("A1:A10")
> colRanges.Add Range("B1:B10")
> colRanges.Add Range("C1:C10")
> For i = 1 To colRanges.Count
> If colRanges.Item(i).Address(False, False) = "B1:B10" Then _
> MsgBox i
> Next i
>
>
> In article <69F81935-1E10-4E77-BE01-(E-Mail Removed)>,
> ExcelMonkey <(E-Mail Removed)> wrote:
>
> > How do you return the object index within a collection. If I have a
> > collection of ranges and I want to loop thru the collection using a For Each
> > loop. What property do I track for each rng to obtain the collection number?
> > Or do I have to use a Counter variable?
> >
> > Set colRanges = New Collection
> >
> > For Each rng2 In colRanges
> >
> > Next
> >
> > Thanks
> > EM

>

 
Reply With Quote
 
JE McGimpsey
Guest
Posts: n/a
 
      10th Jun 2008
I generally prefer to be explicit.

AFAIK, it compiles the same, so it doesn't matter one way or the other
for efficiency.

However, for my own efficiency in reading code I wrote six months ago,
the more explicit I was, the better.

And for anyone who tries to maintain my code and doesn't use the "col"
convention, it's immediately obvious that the variable refers to a
collection rather than an array.

Just my US$0.02.


In article <85E9AEE2-6A05-47C4-900B-(E-Mail Removed)>,
Jim Thomlinson <James_Thomlinson@owfg-Re-Move-This-.com> wrote:

> Just curious. Is there any advantage to using
>
> colRanges.Item(i)
> over
> colRanges(i)
>
> I use the .item when I have the object as part of a with but otherwise I
> tend to omit it.
>
> with colRanges
> .item(i)
> end with

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" camelean@shaw.ca Microsoft ASP .NET 3 22nd Feb 2011 08:06 PM
Adding class object to collection repeats same object through collection? Erazmus Microsoft Excel Programming 2 17th Sep 2007 04:35 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft ASP .NET 1 18th May 2007 10:24 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft Dot NET 1 18th May 2007 10:24 AM
need a collection that retains order and allows InsertAt(index) AND is accessible by key or index Jay B. Harlow [MVP - Outlook] Microsoft VB .NET 1 6th Dec 2004 07:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:27 AM.