I'm trying to create a backup of a Collection so I can return it to its original state after I perform some tasks on it.
Below I've include a sample of my code which (1) creates a collection called Primary, and a second collection called Backup. From a worksheet Sheets("Sheet2").Range("E28:X28"), I take the number from each cell and populate the Primary collection (along with its KEY). Then I attempt to create a backup of the Primary collection. So far, this seems to work perfectly... until I attempt to remove values from the Primary collection and that causes an unexpected result to the Backup collection.
The unexpected result is that whatever I remove from the Primary also is getting removed from the Backup. So it appears that these two collections are somehow linked. However, I need the Backup to be just that... a backup of the ORIGINAL Primary collection so I can go back to the ORIGINAL data to perform an ongoing examination of different combinations. But I need to be able to get back to my original Primary collection in order to accomplish this.
Might anyone have a possible solution to my problem?
Below I've include a sample of my code which (1) creates a collection called Primary, and a second collection called Backup. From a worksheet Sheets("Sheet2").Range("E28:X28"), I take the number from each cell and populate the Primary collection (along with its KEY). Then I attempt to create a backup of the Primary collection. So far, this seems to work perfectly... until I attempt to remove values from the Primary collection and that causes an unexpected result to the Backup collection.
The unexpected result is that whatever I remove from the Primary also is getting removed from the Backup. So it appears that these two collections are somehow linked. However, I need the Backup to be just that... a backup of the ORIGINAL Primary collection so I can go back to the ORIGINAL data to perform an ongoing examination of different combinations. But I need to be able to get back to my original Primary collection in order to accomplish this.
Might anyone have a possible solution to my problem?
VBA Code:
Sub TestCollections()
' Create collection
Dim StrVal As String
Dim collPrimary As New Collection, collBackup As Collection
Dim c As Range
'# Read 20 values to collection
'# NOTE: The following range would simply contain the values of 1, 2, 3.... thru 20; with no duplicates). Ultimately, these can be any number, however, still not duplicates.
'#############################################################
For Each c In Sheets("Sheet2").Range("E28:X28")
'# Conversion to String takes place in order to add a "KEY"
'###########################################################
StrVal = CStr(c.Value)
collPrimary.Add item:=c.Value, Key:=StrVal 'This collection will contain ALL possible player numbers to be used
Next
'# Backup of collection created
'##########################################################################################################
Set collBackup = collPrimary
'# Using the KEY allows me to be specific about which numbers I would like to remove regardless of where they are within the collection
'##########################################################################################################
collPrimary.Remove "1"
collPrimary.Remove "5"
collPrimary.Remove "10"
collPrimary.Remove "17"
End Sub