VBA 函数 - 参数不是可选的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23574083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Function - Argument Not Optional
提问by Robomato
Public Function RETURN_Equipment(Optional category As String) As Collection
Dim config As classConfiguration
Set config = New classConfiguration
Dim item As classItem
Set item = New classItem
Dim myCollection As Collection
Set myCollection = New Collection
For Each config In Configurations
For Each item In config.colItems
If IsMissing(category) Then
myCollection.add item
ElseIf InStr(category, "mainframe") <> 0 And item.category = "mainframe" Then
myCollection.add item
MsgBox "Fired!"
ElseIf category = "accessory" And item.category = "accessory" Then
Else
End If
Next
Next
RETURN_Equipment = myCollection
End Function
I keep getting
我不断得到
Compile error:
Argument not optional
编译错误:
参数不是可选的
I get the error on the last line
我在最后一行收到错误
RETURN_Equipment = myCollection
I understand the error message, its telling me I did not fill out a parameter. But I only have one parameter, and I've declared it optional. It looks like the code thinks I'm trying to call the function from the function?
我理解错误消息,它告诉我我没有填写参数。但是我只有一个参数,而且我已经声明它是可选的。看起来代码认为我正在尝试从函数调用函数?
What gives?
是什么赋予了?
回答by Brad
Anytime you assign an object you need to use the set
keyword.
任何时候分配对象时都需要使用set
关键字。
set RETURN_Equipment = myCollection
set RETURN_Equipment = myCollection
回答by user2023861
I was getting this error because I was using the wrong function name when trying to return a result from a function. I was doing this:
我收到此错误是因为我在尝试从函数返回结果时使用了错误的函数名称。我是这样做的:
Function MyFuncA(arg as String)
MyFuncB = arg 'The problem is I'm using MyFuncB instead of MyFuncA
End Function
This happened because I copied a function from somewhere else and changed the name, but not the return statement. This is not the OP's problem, but I was getting the same error message.
发生这种情况是因为我从其他地方复制了一个函数并更改了名称,但没有更改 return 语句。这不是 OP 的问题,但我收到了相同的错误消息。
回答by Tom Page
Because you've specified the Optional Parameter as a string it will default to an empty string if you've not specified a value.
因为您已将可选参数指定为字符串,所以如果您未指定值,它将默认为空字符串。
This means it can't be missing
这意味着它不能丢失
If you'd specified it as
如果您将其指定为
Public Function RETURN_Equipment(Optional category) As Collection
It would be a variant and that could be missing, although you'd also be able to mess things up by passing non string variants as the category parameter
这将是一个变体并且可能会丢失,尽管您也可以通过将非字符串变体作为类别参数传递来搞砸
The best course of action is probably to replace
最好的做法可能是更换
If IsMissing(category) Then
with
和
If category = "" Then
And as Brad has pointed out you'll need to use Set
正如布拉德指出的那样,你需要使用 Set
Set RETURN_Equipment = myCollection
For full details check this http://msdn.microsoft.com/en-us/library/office/gg251721%28v=office.15%29.aspx
有关完整详细信息,请查看此 http://msdn.microsoft.com/en-us/library/office/gg251721%28v=office.15%29.aspx