Declare dictionary without Microsoft Scripting Runtime

I have a main Excel VBA program that calls a late-bound routine because using the Tools-> References-> Microsoft Scripting Runtime methods is not possible to explain the reasons.

Main Program    
Dim Dict As Object
Set Dict = CreateObject("Scripting.Dictionary")
Call SubRoutine(Dict)

      

The next step is to send the dictionary to my SubRoutine

Public Sub SubRoutine(Dict As Scripting.Dictionary)
do something
end

      

will result in a compilation error which is expected because I have no reference to the Microsoft Scripting Runtime. Questions now how to solve this problem to declare the dictionary. Using

Set Dict = CreateObject("Scripting.Dictionary")

      

Will delete the dictionary.

-1


source to share


1 answer


You are effectively asking how to use late binding instead of early binding ( one article discussing this , other articles ).

Instead of the following, which implies early linking (i.e. setting an explicit reference to the Microsoft Scripting Runtime Library):

Public Sub SubRoutine(Dict As Scripting.Dictionary)

      

you need to declare your Sub parameter as a generic type Object

, as required when using late binding (i.e. there is no explicit reference set, allowing the program to compute values โ€‹โ€‹at runtime):



Public Sub SubRoutine(Dict As Object)

      

Think of it Object

as a container that can contain any object - but once you put an object in it, eg. a Dictionary

, then what it becomes. In the Locals window it will look like Object/Dictionary

, that is, a container Object

(which is pretty much irrelevant to you, don't worry about that), but nature / behavior Dictionary

(which is what you want)

Addendum: This last sentence is not exactly what I thought. See the explanation in the following question: Runtime error using dictionary when using late binding but not early binding

+2


source







All Articles