Collections

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
In this code:

Rich (BB code):
Dim Rng As Range
Set Rng = Sheet1.Range("A1:D4")

Dim RngElement As Range

For Each RngElement In Rng

    RngElement.Value = 100

Next RngElement

it populates every cell in the range A1 to D4 with the value of 100.

I was of the understanding that the variable RngElement MUST be declared as the same type as Rng (in this case as a range), or as a Variant.

Looking at code in this article:

Rich (BB code):
https://excelmacromastery.com/vba-dim/#Using_Dim_with_Class_Module_Objects

in particular this part:

Rich (BB code):
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]' Class Module - clsStudent

Public Name As String
Public Subject As String

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]' Standard Module

Sub ReadMarks()[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    ' Create a collection to store the objects
    Dim coll As New Collection
    
    ' Current Region gets the adjacent data
    Dim rg As Range
    Set rg = Sheet1.Range("A1").CurrentRegion
    
    Dim i As Long, oStudent As clsStudent
    For i = 2 To rg.Rows.Count
        
        ' Check value
        If rg.Cells(i, 1).Value > 50 Then
            ' Create the new object
            Set oStudent = New clsStudent
            
            ' Read data to the student object
            oStudent.Name = rg.Cells(i, 2).Value
            oStudent.Subject = rg.Cells(i, 3).Value
            
            ' add the object to the collection
            coll.Add oStudent
            
        End If
        
    Next i
    
    ' Print the data to the Immediate Window to test it
    Dim oData As clsStudent
    For Each oData In coll
        Debug.Print oData.Name & " studies " & oData.Subject
    Next oData[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]End Sub

oData is declared as clsStudent.

I thought oData has to be declared as a collection because in this next line:

Rich (BB code):
For Each oData In coll


it is looping in a collection.

What am I misunderstanding?

Thanks

<strike>
</strike>
<strike></strike>
[/FONT]
<strike></strike>
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
As far as I can see the collection coll is a collection of clsStudent objects.

So if you were looping through that collection it would make sense that the loop variable, e.g. oData, was the same data type as the objects in the collection, i.e. clsStudent.
 
Last edited:
Upvote 0
I thought oData has to be declared as a collection because in this next line:

Rich (BB code):
For Each oData In coll


it is looping in a collection.

What am I misunderstanding?


Hi

I guess you did not read the help carefully. :)

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.

Check here:

https://docs.microsoft.com/en-us/of...ce/user-interface-help/for-eachnext-statement
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

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