VBA - adding by value to collection

Shads

New Member
Joined
Dec 3, 2014
Messages
5
I want create some objects of a user-defined class (clsDataSet) and put them into a collection. I.e. something like this:
Code:
    Dim col As Range
    Dim allDataPoints As New Collection

    For Each col In RangeIn.Columns
        Dim Series As New clsDataSet
        Series.Initialise (col)
        allDataPoints.Add Series
    Next col
Where clsDataSet.Initialise(ByVal RangeIn as Range) is a function that does various operations to fill the member variables of clsDataSet. The problem is that the objects are added to the collection by reference and so in each loop, every entry in the collection is changed as Series.Initialise() is called. Is there any way of getting round this or adding the objects by value?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,512
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Use a new object instead:
Code:
        Dim Series As clsDataSet
    For Each col In RangeIn.Columns
       Set Series = New clsDataSet
        Series.Initialise col
        allDataPoints.Add Series
    Next col
 

Forum statistics

Threads
1,084,970
Messages
5,380,918
Members
401,705
Latest member
DravenExcel

Some videos you may like

This Week's Hot Topics

Top