twegner
twegner

Reputation: 443

vba Dictionary - returning KEY from Items()

I have a dictionary where each VALUE is another dictionary. In my code I loop through the top-level dictionary using .Items()(i). By the way, it needs to stay this way.

Dim dic As New Scripting.Dictionary
Dim myValue As New Scripting.Dictionary

For i = 0 to dic.count-1
  ' 
  ' the VALUE of the KEY/VALUE pair is...
  set myValue = dic.Items()(i) 
  '
  ' how do I retrieve the KEY???
  '
Next i

My question:

How do I retrieve the KEY of the top-level dictionary in this loop structure? This is probably super obvious, I'm just drawing a blank here.

Upvotes: 4

Views: 21694

Answers (2)

L42
L42

Reputation: 19727

Or maybe this:

Dim key As Variant

With dic
    For Each key In .Keys
        Debug.Print key, .Item(key)
    Next
End With

Upvotes: 8

Tim Williams
Tim Williams

Reputation: 166181

Sub Tester()

    Dim d, i
    Set d = CreateObject("scripting.dictionary")

    d.Add "K1", "v1"
    d.Add "K2", "v2"
    d.Add "K3", "v3"

    For i = 0 To d.Count - 1
        Debug.Print d.items()(i), d.keys()(i)
    Next i

End Sub

Upvotes: 10

Related Questions