1

I'm trying to figure out if this is a possibility in terms of the capability of excel. Consider the following code in what I'm trying to do:

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

The following is the line of code that gives me the error:

some_array_dict(i) = some_dict

Can anyone help with this?

2
  • I think Tim's solution is the only one that actually works. Be careful because Dictionary is a reference type so setting each array component to the same object will result in the same object being referred by all array components. I don't think that this is what you want.. Commented Jun 20, 2014 at 0:31
  • More specifically to Tim's answer, I had to Dim as a Dictionary. That actually ended up fixing the issue I was facing. Thanks for help! Commented Jun 20, 2014 at 14:38

3 Answers 3

4

Array is declared as wrong type, and you need to use Set when assigning objects

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
Sign up to request clarification or add additional context in comments.

Comments

2

It looks like you are trying to assign a Dictionary to a string array.

Try:

ReDim some_array_dict(y) As Dictionary

Comments

2

Since Dictionary is an object, you have to use Set for the assignment:

Set some_array_dict(i) = some_dict ' No issue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.