tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Following from this thread:

Rich (BB code):
https://www.mrexcel.com/forum/excel-questions/1114405-collections.html



I was told this piece of information:

Rich (BB code):
Syntax ... For Each element In group ... element ... For
collections
, element can only be a Variant variable, a generic object variable, or any specific object variable.
So here's my code:

Rich (BB code):
Dim Coll As Collection
    
    Set Coll = New Collection
    
    Coll.Add "Apple"
    Coll.Add "Pear"
    Coll.Add "Orange"
    
    Dim i As Collection
    For Each i In Coll
    
        Debug.Print i
    
    Next i



but it generates an object required error.

According to the article, i can be a Variant, generic object or specific object, so I have declared it here as a Collection (ie a specific object), yet it stil fails.

Why is that?

I've even tried:

Rich (BB code):
Dim Coll As Collection
    
    Set Coll = New Collection
    
    Coll.Add "Apple"
    Coll.Add "Pear"
    Coll.Add "Orange"
    
    Dim i As Collection
    Set i = New Collection
    
    For Each i In Coll
        
        
        Debug.Print i
    
    Next i



but get the same error.

Thanks


<strike></strike>

<strike></strike><strike></strike>
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Maybe

Code:
Sub test()
  Dim [COLOR=#008000]Coll As New Collection[/COLOR],[B][COLOR=#0000ff] i As Variant[/COLOR][/B]
  Coll.Add "Apple"
  Coll.Add "Pear"
  Coll.Add "Orange"
  For Each i In Coll
    Debug.Print i
  Next i
End Sub

Or

Code:
Sub test_2()
  Dim [COLOR=#008000]Coll As New Collection[/COLOR], [B][COLOR=#0000ff]i As Long[/COLOR][/B]
  Coll.Add "Apple"
  Coll.Add "Pear"
  Coll.Add "Orange"
  For i = 1 To Coll.Count
    Debug.Print Coll(i)
  Next i
End Sub
 
Upvote 0
Thanks but why can't I be declared as a collection because according to the article, it can be a variant, generic object or specific object?
 
Upvote 0
The article refers to the elements:
"element ... For collections, element can only be a Variant variable, a generic object variable, or any specific object variable. "

But the group can be declared as a collection
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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