I have a series of data where each item has a number of values associated with it. Blocks of items will share these values, then for other items these will change.
I am transferring over the data between databases. In the old one each item has all of its values stored separately. In the new database I want to take advantage of the fact that large numbers of items share the same values by storing these sets of values as a configuration. I am doing this in vba for excel.
To identify what the unique sets of values are I wanted to use a dictionary where the key is a collection. I got lulled into a false sense of security by it allowing me to do this, however it doesnt manage to identify where keys are identical.
Example code as follows. Should add just two items to the dictionary but adds all 3. Am I missing something or just expecting too much of the dictionary? Would save me a little time if I didnt manually have to compare all the sets.
Sub CollectionAsKeyTest()
Dim dic As New Dictionary
Dim col As Collection
Dim i As Integer
dic.CompareMode = BinaryCompare
'Create a collection to add to dictionary:
Set col = New Collection
For i = 1 To 10
col.Add i * 1
Next i
dic.Add col, "item 1"
'Create a different collection and add as key to dictionary:
Set col = New Collection
For i = 1 To 10
col.Add i * 2
Next i
If Not dic.Exists(col) Then dic.Add col, "item 2"
'Create a collection which is the same as the first, and try to add to dictionary:
Set col = New Collection
For i = 1 To 10
col.Add i * 1
Next i
If Not dic.Exists(col) Then dic.Add col, "item 3"
'All three collections are added:
Debug.Print "Number of collections added = " & dic.count
End Sub
Key? Store your collection as theValueand yourValueas theKeyRange("A1")isn't the same asRange("A2")even ifRange("A1").Value = Range("A2").Value. Collections are objects.