vba:从函数返回字典

jas*_*n m 11 excel vba excel-2003 excel-vba

这概述了我想要做的事情.

这对我不起作用,目前还不清楚为什么.

提前感谢您的帮助.

        Sub mySub()
        dim myDict as Dictionary
            myDict=new Dictionary

                myDict=myFunc()

        End Sub

        Function myFunc()
            dim myDict2
                set myDict2 = new Dictionary

                    'some code that does things and adds to myDict2'

            myFunc=myDict2
        End Function
Run Code Online (Sandbox Code Playgroud)

Bra*_*adC 33

在分配对象而不是值时,您需要使用SET关键字:

    Sub mySub()
        dim myDict as Dictionary
        set myDict = myFunc()
    End Sub

    Function myFunc() as Dictionary
        dim myDict2 as Dictionary
        set myDict2 = new Dictionary
                'some code that does things and adds to myDict2'
        set myFunc=myDict2
    End Function
Run Code Online (Sandbox Code Playgroud)

您的原始代码也将myDict创建为新的Dictionary对象,然后立即将其替换为另一个.你可以跳过这一步.