将对象传递给Excel VBA Sub会导致参数非可选错误



我有以下子:

Public Function Log(sdsMessage As CStandardPropertySet2)
' Only print the fields contained int he sdsMessage
End Function

我这样使用它:

Log(anSdsMessage)

然而,这会导致Argument not optional错误。

但如果我有这样的潜艇:

Public Function Log(sdsMessage As CStandardPropertySet2) As CStandardPropertySet2
' Only print the fields contained int he sdsMessage
Set Log = sdsMessage
End Function
Set anSdsMessage = Log(anSdsMessage)

然后错误消失。这对我来说看起来很尴尬,因为我根本不需要修改消息,只想打印其中的字段。

有人能告诉我我做错了什么吗?

这是愚蠢的混淆VBA语法。当jsut将Log作为方法调用并且没有返回类型时,可以省略括号。

所以当没有设置返回值时Log anSdsMessage

当设置返回值时(正确地说)Set anSdsMessage = Log(anSdsMessage)

针对Daniel的评论

我可以写这个代码

Public Function Log(sdsMessage As Collection)
' Only print the fields contained int he sdsMessage
End Function
Sub tester()
Dim o As Collection
Set o = New Collection
Log (o)
End Sub

尝试运行tester子系统,我得到错误消息"编译错误:参数不是可选的">

删除括号并运行Log o实际上允许编译并删除错误。

最新更新