有没有可能在Excel VBA中添加字典到数组?

我试图弄清楚,这是否是一个可能性方面的能力的Excel。 考虑下面的代码,我想要做什么:

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 

有人能帮忙吗? 让我知道,如果你需要任何澄清这一点。

感谢您的帮助。

它看起来像你正试图分配一个字典到一个string数组。

尝试:

 ReDim some_array_dict(y) As Dictionary 

数组被声明为错误types,并且在分配对象时需要使用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 

由于Dictionary是一个对象,所以你必须使用Set来赋值:

 Set some_array_dict(i) = some_dict ' No issue