Can a dictionary object have multiple Items under same key?

后端 未结 4 563
不思量自难忘°
不思量自难忘° 2021-01-19 01:49

I a looking for an workaround related to dictionary objects items

Dim a, d \'Create some variables

 Set d = CreateObject(\"Scripting.Dictionary\")

 d.Add \         


        
相关标签:
4条回答
  • 2021-01-19 01:53

    EDIT: Makes variable name more OPfriendly

    EDIT: Include a Scripting.Dictionary Reference for OP's reading

    Sub t()
        Dim d
        Dim a
        a = Array("Athens", "India", "Paris")
        Set d = CreateObject("Scripting.Dictionary")
        d.Add "a", a
        MsgBox Join(d.Item("a"), ",")
    End Sub
    

    EDIT: to read values as in OP's question into dictionary and retrieve the values

    Sub t()
        Dim d As Object
        Dim values
        Dim height As Long
        Dim item
        Dim width As Long
        Dim i As Long, j As Long
        Dim MANGERID
        Dim EMPIDs
        Dim EMPID
        With ActiveSheet
            height = .Cells(.Rows.Count, 1).End(xlUp).Row
            If height < 2 Then
                Exit Sub
            End If
            Set d = CreateObject("Scripting.Dictionary")
            For i = 2 To height
                width = .Cells(i, .Columns.Count).End(xlToLeft).Column
                if width > 1 then  'make sure have EMPID
                    ReDim values(1 To width - 1)
                    Key = .Cells(i, 1).Value
                    For j = 2 To width
                        values(j - 1) = .Cells(i, j).Value
                    Next j
                    d.Add Key, values
                end if
            Next i
    
            'displaying back the items in the dictionary
            For Each MANGERID In d.keys
                'value array
                EMPIDs = d.item(MANGERID)
                If TypeName(EMPIDs) = "Variant()" Then
                    For Each EMPID In EMPIDs
                        Debug.Print MANGERID & ":" & EMPID
                    Next EMPID
                End If
            Next MANGERID
    
            Set d = Nothing
        End With
    End Sub
    
    0 讨论(0)
  • 2021-01-19 01:56

    If you try to add more than one item in one key their would be the duplication of key and Vb Script does not allow it. To know more information on dictionary object refer the following link. How To Use Dictionary Object

    0 讨论(0)
  • 2021-01-19 02:01

    No. By definition, dictionary data types use keys that must be unique. I know it's like this on most implementations, but the closest I can come to for an authoritative reference for VBscript's Scripting.Dictionary is this TechNet blurb:

    A key is a unique entry: no two keys within a single Dictionary object can be the same.

    0 讨论(0)
  • 2021-01-19 02:15

    Here is another example for any others who view this and need another approach towards this solution. I feel this can be of help.

    Dim objDictionary
    Set objDictionary = CreateObject("Scripting.Dictionary")
        dicKey = 69
        dicValues = ""
        dicVal = "val1"
        dicVal2 = "val2"
        dicVal3 = "val3"
        objDictionary.add dicKey, DicValues1 & DicValues2
        chang = -1
    
    if chang = -1 then  
        objDictionary.item(dicKey) = dicVal & "," & dicVal2
        chang = -69
    end if
    
    if chang = -69 then
        objDictionary.item(dicKey) = dicVal3 & ", " & dicVal & ", " & dicVal2
        chang = -1
    end if
    
    for each objDictionary_key in objDictionary.keys
        msgbox "Key: " & objDictionary_Key & " Value: " & objDictionary.item(objDictionary_Key)
    next 
    

    I hope this helps some!

    0 讨论(0)
提交回复
热议问题