2014-06-19 56 views
0

我试图弄清楚,如果这是一个可能性方面的能力的Excel。考虑什么,我试图做下面的代码:是否可以在Excel VBA中将数组字典添加到数组中?

Dim some_text, split_text() As String 
Dim some_array_dict() As String 'This is the array to store the dictionaries 
Dim some_dict As Dictionary 'The dictionaries that I'll be storing in the array above 

ReDim some_array_dict(y) As String 'y is previously defined as an integer 

For i = 0 To y - 1 
    Set some_dict = New Dictionary 

    some_text = some_array(2, i) 
    split_text = Split(some_text, " ") 

    For j = 0 To UBound(split_text) 
     some_dict.Add split_text(j), 1 
    Next j 

    some_array_dict(i) = some_dict 'Issue 

    Debug.Print some_array_dict(i) 'For debugging purposes 
Next i 

以下是代码,给我的错误行:

some_array_dict(i) = some_dict 

任何人都可以在这方面帮助?让我知道你是否需要任何澄清。

感谢您的帮助。

+0

我觉得Tim的解决方案是唯一一个实际上有效。要小心,因为[Dictionary是一个引用类型](http://stackoverflow.com/questions/3022182/how-do-i-clone-a-dictionary-object),因此将每个数组组件设置为相同的对象将导致相同的对象被所有数组组件引用。我不认为这是你想要的.. – Ioannis

+0

更具体地说,蒂姆的答案,我不得不''昏暗'作为一个'字典'。这实际上最终解决了我面临的问题。感谢帮助! – lcardona89

回答

0

它看起来像你试图分配一个字典字符串数组。

尝试:

ReDim some_array_dict(y) As Dictionary 
+0

这最终成为我特别面临的问题。谢谢您的帮助! – lcardona89

2

数组被声明为错误的类型,你需要使用Set分配对象

Sub Tester() 

    Dim arr_dict() As Object, x As Long 

    ReDim arr_dict(1 To 3) 
    For x = 1 To 3 

     Set arr_dict(x) = CreateObject("scripting.dictionary") 

     With arr_dict(x) 
      .Add "hello", 1 
      .Add "there", 2 
     End With 

    Next x 

End Sub 
0

时由于Dictionary是一个对象,你必须使用Set的分配:

Set some_array_dict(i) = some_dict ' No issue 
相关问题