检索vba中的一个dictionnary的集合types元素

我在vba中有一个dictionnary,每个键都是一个string,每个值都是一个集合,这是我的代码:

Dim dict As Dictionary Set dict = New Dictionary Set collec=New collection Dim i As Integer For i=1 To 5 collec.Add "element" & i Next i dict.Add "mykey",collect 

这是我被封锁的地方,我想利用这样的关键利用集合:

 Dim test As New collection test=dict("mykey") 

但是这不起作用

在此先感谢您的帮助

分配对象时需要使用Set关键字。 你的代码中也有一些拼写错误( collec vs collect )。 您应该使用Option Explicit来捕获这些types的错误:

 Option Explicit Dim dict As Dictionary Set dict = New Dictionary Set collec = New collection Dim i As Integer For i=1 To 5 collec.Add "element" & i Next i dict.Add "mykey", collec 'fix typo Dim test As Collection Set test = dict("mykey") 'use Set to assign object 
 Sub M_snb() With CreateObject("scripting.dictionary") For j = 1 To 5 Set .Item("c_" & .Count) = New Collection Next .Item("c_2").Add "first" .Item("c_2").Add "second" .Item("c_2").Add "third" MsgBox .Item("c_2").Item(2) End With End Sub