Excel vba:Class sub:vba上的参数错误数量或属性赋值无效

我有一个类的State和一些子里面,它把一个Scripting.Dictionary作为参数。 但是,当我尝试传递一个字典时,我得到一个wrong number of arguments or invalid property assignment错误。 我无法弄清楚什么是错的。

 'Sub insite State class Sub addSecondItems(itemsDict As Object) MsgBox ("start addSecondItems") End Sub Sub test() Dim stateCopy As State Set stateCopy = New State ... Dim dict1 As Object Set dict1 = CreateObject("Scripting.Dictionary") stateCopy.addSecondItems (dict1) 'error here ... End Sub 

与此同时

 Sub testPetDict() Dim petDict As Object Set petDict = CreateObject("Scripting.Dictionary") Call readPetDict(petDict) End Sub Sub readPetDict(petDict As Object) Dim year As Integer For year = 2014 To 2017 MsgBox (year & ". " & petDict(year)) Next End Sub 

工作正常。

这里有什么可能是错的,为什么第二个案件起作用,而第一个案件失败?

你应该删除括号:

 stateCopy.addSecondItems dict1 

或使用Call

 Call stateCopy.addSecondItems(dict1) 

否则括号试图通过调用它的默认属性Item来强制字典值为一个值,它需要一个参数,因此是错误信息。