Selecting Item of Dictionary by Index # using VBA in Excel

collections, dictionary, vba

Solution

The behaviour is different because a Dictionary allows numeric keys.

Calling `.Item` actually adds an item with the given key so;

NodeColl.Item(NodeColl.Count)

Adds a new item with no value & a key corresponding to the count.

To access the ordinal item use `.Items` (which is an array of the items)

firstItem = NodeColl.Items(0)
lastItem  = NodeColl.Items(NodeColl.Count - 1)

Problem

I am trying to load a combo box with the last item in a dictionary. I am trying to do something like this `ComboBox1.Value = NodeColl.Item(NodeColl.Count)` which would work with a collection, but does something strange when using a dictionary instead.

Original source