I have two collections - collection1
and collection2
collection1
has a number of class objects in it and I am trying to fill collection2
with copies of the same objects using the following command:
Set collection2 = collection1
This doesn't give me the desired result though, because when I use
collection2.Remove 1
It removes the object at index 1 from both collections.
Below is the full code, which I hoped would output 10
objects in collection1
after removing one from collection2
Sub test()
Dim collection1 As Collection
Dim collection2 As Collection
Dim testObj As Worksheet
Dim i As Integer
Set collection1 = New Collection
Set collection2 = New Collection
For i = 1 To 10
collection1.Add testObj
Next i
Set collection2 = collection1
collection2.Remove 1
Debug.Print collection1.Count
End Sub
I tried the code below and it works but I'm looking to avoid filling both collections one by one if possible:
...
For i = 1 To 10
collection1.Add testObj
collection2.Add testObj
Next i
...
The reason I'm not so keen on this option is because ultimately I intend to use multiple collections, manipulating them and taking copies at various points so I would end up with lots of for loops in my code, rather than just one.