Excel vba:类子: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需要一个参数,因此会出现错误消息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章