字典对象可以在同一个键下有多个项吗?

aru*_*roy 5 vbscript

我正在寻找与字典对象项相关的解决方法

Dim a, d 'Create some variables

 Set d = CreateObject("Scripting.Dictionary")

 d.Add "a", "Athens" 'is possible I know 

 d.Add "a", "Athens","India","Paris" ' Is this Possible  under same Key?
Run Code Online (Sandbox Code Playgroud)

描述快照:( 更新)

  Manger ID            EMPID      EMPID     EMPID     EMPID ......

     11                12          10
     15                10 
     20                22          45        46
     40
Run Code Online (Sandbox Code Playgroud)

如何使用字典对象实现上表?给我一些想法.

谢谢,

Lar*_*rry 4

编辑:使变量名称更加OP友好

编辑:包括 Scripting.Dictionary 参考以供 OP阅读

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
Run Code Online (Sandbox Code Playgroud)

编辑:将OP问题中的值读入字典并检索值

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
Run Code Online (Sandbox Code Playgroud)