Can I loop through key/value pairs in a VBA collection?

64,591

you cannot retrieve the name of the key from a collection. Instead, you'd need to use a Dictionary Object:

Sub LoopKeys()
    Dim key As Variant

    'Early binding: add reference to MS Scripting Runtime
    Dim dic As Scripting.Dictionary
    Set dic = New Scripting.Dictionary

    'Use this for late binding instead:
    'Dim dic As Object
    'Set dic = CreateObject("Scripting.Dictionary")

    dic.Add "Key1", "Value1"
    dic.Add "Key2", "Value2"

    For Each key In dic.Keys
        Debug.Print "Key: " & key & " Value: " & dic(key)
    Next
End Sub
Share:
64,591

Related videos on Youtube

Peter Rankin
Author by

Peter Rankin

I am an independent software developer in South Carolina. I enjoy all types of software engineering, and I especially enjoy building web applications for data management. Developing software through all stages, including testing and debugging, has given me a much greater appreciation for high-quality software development. In particular, it has given me an appreciation for the best software engineer in history--God. After all, He programmed us. He wrote computer programs (in our brain) that could write other computer programs! I'm so glad that He has provided a way for us to be forgiven of our sins by faith in Jesus Christ, His only Son, and the only way to Heaven. Indeed, I can agree with David in the Bible when he says, "I will praise thee; for I am fearfully and wonderfully made: marvellous are thy works; and that my soul knoweth right well."

Updated on January 29, 2021

Comments

  • Peter Rankin
    Peter Rankin over 3 years

    In VB.NET, I can iterate through a dictionary's key/value pairs:

    Dictionary<string, string> collection = new Dictionary<string, string>();
    collection.Add("key1", "value1");
    collection.Add("key2", "value2");
    
    foreach (string key in collection.Keys)
    {
        MessageBox.Show("Key: " + key + ".  Value: " + collection[key]);
    }
    

    I know in VBA I can iterate through the values of a Collection object:

    Dim Col As Collection
    Set Col = New Collection
    Dim i As Integer
    Col.Add "value1", "key1"
    Col.Add "value2", "key2"
    
    For i = 1 To Col.Count
        MsgBox (Col.Item(i))
    Next I
    

    I also know that I do this with a Scripting.Dictionary VBA object, but I was wondering if this is possible with collections.

    Can I iterate through key/value pairs in a VBA collection?

    • Dmitry Pavliv
      Dmitry Pavliv over 10 years
      As I know, you can't..You should use Dictionary if you want to iterate through key/value pairs..
  • Melroy van den Berg
    Melroy van den Berg almost 10 years
    Thanks for the early binding, new Dictionary didn't work. But New Scripting.Dictionary did :D Thx again.
  • Johnson Jason
    Johnson Jason over 8 years
    doesn't work in my VBA script. Am I needing a lib or something?
  • Peter Albert
    Peter Albert over 8 years
    @JohnsonJason: yes, to Microsoft scripting runtime as mentioned in the commented code - or use the commented late binding code, then no reference is required
  • Ken
    Ken about 8 years
    This is an excellent answer and for the fact that you showed the example with both early and late binding - making it clear and also informing others of the difference . Wish I could plus 10 you!