@AB33 apologies if I sounded condescending, that wasn't my intention. I merely trying to set the complexity of collections in the grand scheme of things.
The easiest way to think about collections is as an untyped associative array. Untyped meaning that they aren't fussy about what you stick in them whereas an array of integers would only allow integers for example and associative as in they have strings that you can refer to the elements by.
So whereas a normal array is just a list of data that you can access by knowing its index (position) a collection as well as allowing you to access its elements by index also allow you to give them a reference to call them by, for example:
Array of Doubles:
1.00
2.00
3.00
4.00
Access of array syntax: array(x)
Accessing 2nd element: array(1)
Collection:
"Kyle" 1.00
"AB33" 2.00
"James" 3.00
"john" 4.00
Access of collection syntax: Collection(x) - or - Collection("key")
Accessing 2nd element: Collection(2) - or - Collection("AB33")
The caveat is that all keys must be unique for obvious reasons, collections are useful in that you can add to them, remove from them by key or index and insert into them between 2 items. You don't need to know how many elements you will put in it in advance like you would with an array, you can just add more elements. This does come at a cost though, collections are slower than arrays more noticeably as the collections get larger since each time you add something to a collection, the whole collection gets re-indexed behind the scenes.
Collections are limited in that although you can reference an element by its key, you cannot return the keys from the collection. So you can't check to see if an element exists.
Dictionaries function in the same way as collections, but allow you to return the keys therefore to check if an element exists by its key. Dictionaries also allow you to remove all its elements.
So why aren't they used more? Well it's horses for courses, collection objects are slower than arrays, they're also less useful in most scenarios. For example, you can't assign a range of values to a collection object like you can a variant array:
dim var
var = sheets(1).currentregion.value
You have to loop through the whole range/array and add items individually and you can't use Excel functions on them e.g
, you can't use functions like join and filter etc on a collection either
You do often see them used for getting unique elements from a range, this works like this:
Dim oCell As Range
Dim coll As Collection: Set coll = New Collection
For Each oCell In Sheets(1).Range("a1:b10")
On Error Resume Next
coll.Add oCell.Value, oCell.Value
Next oCell
Since keys have to be unique, the collection.add will fail for duplicate values and the element will not be added (that's why On Error resume Next is needed) - remember you can't check to see if a key exists in a collection.
The same thing but for a Dictionary:
Dim oCell As Range
Dim dic As Dictionary: Set dic = New Dictionary
For Each oCell In Sheets(1).Range("a1:b10")
If Not dic.Exists(oCell.Value) Then
dic.Add oCell.Value, oCell.Value
End If
Next oCell
Of course with both the above, if we wanted to write the data back to the sheet we'd have to loop through the full object - you can't assign an array to a collection or vice versa
This is more elegant since we can check if a key exists and if so, not add it again (no reliance on errors). So as I mentioned earlier, dictionaries are just richer collections. Collection objects are not hugely complicated, but the way they're used can be.
Here's a coincidence, a collection question I answered this morning: http://www.excelforum.com/excel-prog...rom-range.html
Hope this helps
Bookmarks