Lucky0683
Lucky0683

Reputation: 123

Multiple dictionaries for each unique key (VBA)

Is it possible to have multiple dictionaries that automatically add keys ending with the same number into it?

What i have in mind is something like this:

dicInfo (i). add (key (i), value)

so when there are keys like key1, key2, key3, they are added to dicInfo1, dicInfo2 etc?

Thanks!

Upvotes: 1

Views: 1272

Answers (1)

TinMan
TinMan

Reputation: 7759

Here is how to add Dictionaries to a Dictionary and return the values

Sub Demo()
    Dim dic As Object, key As Variant
    Dim j As Long
    Set dic = CreateObject("Scripting.Dictionary")

    For j = 1 To 100
        key = CLng(Right(j, 1))
        If Not dic.Exists(key) Then dic.Add key:=key, Item:=CreateObject("Scripting.Dictionary")
        dic(key).Add dic(key).Count, j
    Next

    For j = 0 To 9
        Debug.Print Join(dic(j).Keys(), ",")
        Debug.Print Join(dic(j).Items(), ",")
    Next

End Sub

enter image description here

Upvotes: 1

Related Questions