字典对象可以在同一个键下有多个项目吗? [英] Can a dictionary object have multiple Items under same key?

查看:28
本文介绍了字典对象可以在同一个键下有多个项目吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I a looking for an workaround related to dictionary objects items

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?

描述快照:(更新)

Description snapshot:(UPDATED)

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

     11                12          10
     15                10 
     20                22          45        46
     40

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

How the above table can then be implemented using dictionary object? Give me some idea.

谢谢,

推荐答案

使变量名称更易于操作

Makes variable name more OPfriendly

包括 OP 的 Scripting.Dictionary 参考 阅读

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

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

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

这篇关于字典对象可以在同一个键下有多个项目吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆