Loop through a "specific" property of the objects in a collection

rkaczano

Board Regular
Joined
Jul 24, 2013
Messages
141
Office Version
  1. 365
Platform
  1. Windows
I know I can loop though objects in a collection.

Is it possible to loop through a "specific" property of the objects in that collection?

For example. I have 500 instances of an object (obj) and I also have a custom collection (coll) and I would loop as follows through the objects in the collection.

For each obj in coll
'Do something
Next

Each obj has a property called OType:
300x instances have OType = "A"
100x instances have OType = "B"
75x instances have OType = "C"
25x instances haveOType = "D"

I want to be able to loop through those 4 distinct property values in the collection. This would loop 4 times instead of 500 times. The following does not work.

For each obj.OType in coll
'Do something
Next

Thanks
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
VBA will not dynamically look at object attributes for you and analyze them to tell you the range of values in that attribute. So yeah, what you tried can't work.

How did the A, B, C, and D values get assigned in the first place? That is the place you need to think about how to loop these four values. You may need to define those four values as their own collection, and you loop through that collection. Or an array. It's hard to get more specific without knowing how you determine and assign the value to each object.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top