Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass vba Dictionary

Tags:

excel

vba

New to vba. How to pass dictionary object to another function.

Sub aaa(dict As Object)
Set dict = CreateObject("Scripting.Dictionary")
...
process dict 
End Sub

Sub process(dict As Scripting.Dictionary)
    MsgBox dict.Count
End Sub

gives a compile error: User defined type not defined


Also,

Set dict = CreateObject("Scripting.Dictionary")

works, but

Dim dict As New Scripting.Dictionary 

gives, "User defined type not defined"

I use excel 2010

like image 608
bsr Avatar asked Feb 22 '12 19:02

bsr


1 Answers

When you use CreateObject you are binding the object at run-time (ie, late binding). When you use As Scripting.Dictionary the object is bound at compile-time (ie, early binding).

If you want to do early binding you will need to set a reference to the correct library. To do this, go to Tools --> References... and select "Microsoft Scripting Runtime"

like image 196
mwolfe02 Avatar answered Sep 20 '22 14:09

mwolfe02