Unable to pass dictionary as argument to VBA function

I have a VBA function that should use a dictionary as an argument:

Function ShowDict(Dict1 As Dictionary)
   For Each x In Dict1
        MsgBox (Dict1.Item(x))
   Next
End Function

And I'm trying to call it this way:

Dim Dict As Dictionary
Set Dict = Dictionary
Dict.Add "Owner", "John"
Dict.Add "Employee", "Sam"
ShowDict (Dict)

I selected the Microsoft Scripting References links from the links before the dictionary definition. However, I get a compilation error indicating "The argument is not optional " when I try to call a function using the "Dict" parameter as a parameter. Can anybody help me?

+4
source share
1 answer

Make these two changes ( New Dictionaryand ShowDict Dict):

Dim Dict As Dictionary
'Set Dict = Dictionary
Set Dict = New Dictionary
Dict.Add "Owner", "John"
Dict.Add "Employee", "Sam"
'ShowDict (Dict)
ShowDict Dict

Option Explicit Declarations Dim x ShowDict. , .

+5

All Articles