VBA我可以将收藏夹放在scripting.dictionary中吗 [英] vba can i put a collection inside a scripting.dictionary

查看:90
本文介绍了VBA我可以将收藏夹放在scripting.dictionary中吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个收藏夹并放入里面

I am trying to create a collection and put inside

mydict = my_key:["a", "b", "c"]

然后遍历.

    For Each V In mydict.keys
        Debug.Print V
        for z in mydict.Item(V)       
            Debug.Print  z
        next z
    Next V

输出应该看起来像 我的钥匙 一种 b c

the output should look like my_key a b c

但是我遇到了问题和错误,是否可以将集合存储在scripting.dictionary中?

but i having problems and errors, is it posible to store a collection inside a scripting.dictionary?

或者只是存储字符串,例如"my_key":"a","my_key2":"b"?

or is just storing strings such "my_key":"a", "my_key2":"b"?

谢谢.

推荐答案

要将集合存储在字典中,可以使用两步过程,首先创建集合,然后将其添加到字典中:

To store a collection in a dictionary you can use a two-step process of first creating the collection and then adding it to the dictionary:

Sub test()
    Dim C As Collection
    Dim D As Object
    Dim v As Variant

    Set D = CreateObject("Scripting.Dictionary")

    Set C = New Collection
    C.Add "a"
    C.Add "b"
    C.Add "c"

    D.Add "key1", C

    Set C = New Collection 'Old collection safely stored in D
    D.Add "key2", C 'Now D("key2") holds a collection
    D("key2").Add "d"
    D("key2").Add "e"

    Debug.Print "Collection for key1:"
    For Each v In D("key1")
        Debug.Print v
    Next v

    Debug.Print "Collection for key2:"
    For Each v In D("key2")
        Debug.Print v
    Next v

End Sub

代码说明了如何添加回收集合变量C以添加多个集合,以及如何向字典中添加空集合以供以后修改.

The code illustrates how you can add recycle the collection variable C to add multiple collections, and how you can add empty collections to the dictionary to be later modified.

输出:

Collection for key1:
a
b
c
Collection for key2:
d
e

这篇关于VBA我可以将收藏夹放在scripting.dictionary中吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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