I know this is a simple question but it’s aggravating me. If I have a key/value pair in a collection but I can’t seem to get the value out using the key. I can get the key using the value but not vice versa. Is there some magical way to accomplish this?
For example:
Dim CycleList As Collection
Dim Value as String
Set CycleList = New Collection
CycleList.Add 1, "Some Value"
Value = CycleList(1)
I’ve also tried CycleList.Item(1) and it’s the same result, Value = 1.
The reason is that you are telling VBA to add an integer 1 with an alternate key of
Some Valueto the collection. When you callCycleList(1), you are asking for theItemwith an index of 1 which happens to be the value 1. The second parameterSome Valuerepresents an alternate key you can use to find the item you want. Here’s an example to illustrate:Results when calling Foo in the debug window:
Note that in the first Debug.Print, I ask for the value by key but in the others, I’m asking for the value by index.