• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Create/Accessing Dictionary within Dictionary

Chihiro

Excel Ninja
I'm trying to add Scripting.Dictionary within another Dictionary.

This is related to Deepak's thread below.
http://chandoo.org/forum/threads/count-the-records.28411/

So, to only add unique records to each bucket, I'm using Dictionary to keep track.

However, my initial code (which is in Module1) is not dynamic at all. I'm thinking using array of bucket names to create Dictionaries in Dictionary to dynamically create and access.

Referenced below thread.
http://www.mrexcel.com/forum/excel-...basic-applications-creating-objects-loop.html

I've got to a point where I was able to add items dynamically to Dictionary. But not sure how to access the dictionary within (or to check if it's a dictionary). I was able to retrieve .Key from "myObjects", but not the data within.


Any help is appreciated. Code below and workbook attached (the code is in Module2).

FYI - Microsoft Scripting Runtime is referenced in the project.

Code:
Sub MakeObjects()
    Dim myObjects As New Scripting.Dictionary
    Dim dObject As Scripting.Dictionary
    Dim darry() As String
    Dim dName As Variant
    Dim i As Long, j As Long, lRow As Long, cCount As Long
    Dim sWs As Worksheet
   
    Set sWs = Sheet4
    lRow = sWs.Range("A" & Rows.Count).End(xlUp).Row
    cCount = lRow / 100
   
    ReDim darry(1 To cCount)
    For i = 1 To cCount
        darry(i) = "Bucket" & i
    Next i
   
    For j = 1 To UBound(darry)
        Set dObject = New Scripting.Dictionary
        dName = darry(j)
        myObjects.Add Item:=dObject, Key:=dName
    Next
   
    Set dObject = Nothing
    myObjects.Item("Bucket4").Add Item:=1, Key:="test"
    Debug.Print myObjects.Item(3)
End Sub
 

Attachments

Thanks Narayan :)

Didn't know array could be objects (to be honest, I just started learning to use array). It should work nicely.

I also figured out how to access dictionary within dictionary. I changed the code slightly to have sequential numeric key for "myObjects". I'm thinking this would make it easier to loop through each dictionary contained within.

Code:
Sub MakeObjects()
    Dim myObjects As New Scripting.Dictionary
    Dim dObject As Scripting.Dictionary
    Dim darry() As String
    Dim dName As Variant
    Dim i As Long, j As Long, c As Long, lRow As Long, cCount As Long
    Dim sWs As Worksheet
    Dim cel As Range
  
  
    Set sWs = Sheet4
    lRow = sWs.Range("A" & Rows.Count).End(xlUp).Row
    cCount = lRow / 100
  
    ReDim darry(1 To cCount)
    For i = 1 To cCount
        darry(i) = "Bucket" & i
    Next i
  
    For j = 1 To UBound(darry)
        Set dObject = New Scripting.Dictionary
        dName = darry(j)
        myObjects.Add Item:=dObject, Key:=j
    Next
  
    myObjects(1).Add Item:="Test", Key:=1
    Debug.Print myObjects(1).Item(1)
End Sub
 
Back
Top