VBA 带有可选参数的调用方法
VBA Call method with optional parameters
我刚刚发现设置可选参数需要在方法前面 "Call"。
Public Sub Test()
Call abc("aaa")
Call abc("aaa", 2)
abc("aaa") ' is fine
abc("aaa", 2) ' is a syntax error
End Sub
Function abc(a As String, Optional iCol As Long = 3)
MsgBox (iCol)
End Function
你能在我的新信息中添加一个"why does this make sense?"吗?
您好,
彼得
编辑:PS 函数 abc 除了简化问题外别无他用。
文档
Call
是一个可选关键字,但需要注意的是,如果您使用它,则必须在参数周围包含括号,但如果您省略它,则不得包含括号。
引自 MSDN:
You are not required to use the Call keyword when calling a procedure.
However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.
To pass a whole array to a procedure, use the array name followed by empty parentheses.
Link: https://msdn.microsoft.com/en-us/library/office/gg251710.aspx
实践
这意味着允许使用以下语法:
Call abc("aaa")
Call abc("aaa", 2)
abc "aaa", 2
abc("aaa") ' <- Parantheses here do not create an argument list
abc(((("aaa")))) ' <- Parantheses here do not create an argument list
不允许使用以下语法:
Call abc "aaa", 2
abc("aaa", 2) ' <- Parantheses here create an argument list
函数Return值
这在使用函数获取 return 值时不会生效,例如,如果您要执行以下操作,则需要括号:
Function abc(a As String, Optional iCol As Long = 3)
abc = iCol
End Function
'## IMMEDIATE WINDOW ##
?abc("aaa", 2) 'this works
?abc "aaa, 2 'this will not work
?Call abc "aaa", 2 'this will not work
?Call abc("aaa", 2) 'this will not work
如果您在 Function
上使用 Call
,请考虑将其更改为 Sub
,函数意味着 return 上述情况中的值.
像这样的东西会起作用:
Option Explicit
Public Sub Test()
Dim strText As String
strText = "E"
Call Abc(strText, 3)
Call Abc(strText, 2)
Abc (strText)
' Abc (strtext,5)
Abc strText, 2
Abc strText
End Sub
Public Sub Abc(strText As String, Optional iCol As Long = 5)
Debug.Print iCol
End Sub
完全不知道为什么注释代码不起作用...
Call
是一个可选关键字,如上面的答案中已详细描述的那样。
第二个选项
abc("aaa", 2) ' is a syntax error
只需使用:
abc "aaa", 2
注意:如果你不return任何东西,Function
几乎没有用,你可以有一个常规的Sub
相反。
有一个 Function
那个 return 一个 String
例如(只是快速编造的东西):
Function abc(a As String, Optional iCol As Long = 3) As String
abc = a & CStr(iCol)
End Function
然后调用它:
Public Sub Test()
d = abc("aaa", 2)
MsgBox d
End Sub
我刚刚发现设置可选参数需要在方法前面 "Call"。
Public Sub Test()
Call abc("aaa")
Call abc("aaa", 2)
abc("aaa") ' is fine
abc("aaa", 2) ' is a syntax error
End Sub
Function abc(a As String, Optional iCol As Long = 3)
MsgBox (iCol)
End Function
你能在我的新信息中添加一个"why does this make sense?"吗?
您好, 彼得
编辑:PS 函数 abc 除了简化问题外别无他用。
文档
Call
是一个可选关键字,但需要注意的是,如果您使用它,则必须在参数周围包含括号,但如果您省略它,则不得包含括号。
引自 MSDN:
You are not required to use the Call keyword when calling a procedure.
However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.
To pass a whole array to a procedure, use the array name followed by empty parentheses.
Link: https://msdn.microsoft.com/en-us/library/office/gg251710.aspx
实践
这意味着允许使用以下语法:
Call abc("aaa")
Call abc("aaa", 2)
abc "aaa", 2
abc("aaa") ' <- Parantheses here do not create an argument list
abc(((("aaa")))) ' <- Parantheses here do not create an argument list
不允许使用以下语法:
Call abc "aaa", 2
abc("aaa", 2) ' <- Parantheses here create an argument list
函数Return值
这在使用函数获取 return 值时不会生效,例如,如果您要执行以下操作,则需要括号:
Function abc(a As String, Optional iCol As Long = 3)
abc = iCol
End Function
'## IMMEDIATE WINDOW ##
?abc("aaa", 2) 'this works
?abc "aaa, 2 'this will not work
?Call abc "aaa", 2 'this will not work
?Call abc("aaa", 2) 'this will not work
如果您在 Function
上使用 Call
,请考虑将其更改为 Sub
,函数意味着 return 上述情况中的值.
像这样的东西会起作用:
Option Explicit
Public Sub Test()
Dim strText As String
strText = "E"
Call Abc(strText, 3)
Call Abc(strText, 2)
Abc (strText)
' Abc (strtext,5)
Abc strText, 2
Abc strText
End Sub
Public Sub Abc(strText As String, Optional iCol As Long = 5)
Debug.Print iCol
End Sub
完全不知道为什么注释代码不起作用...
Call
是一个可选关键字,如上面的答案中已详细描述的那样。
第二个选项
abc("aaa", 2) ' is a syntax error
只需使用:
abc "aaa", 2
注意:如果你不return任何东西,Function
几乎没有用,你可以有一个常规的Sub
相反。
有一个 Function
那个 return 一个 String
例如(只是快速编造的东西):
Function abc(a As String, Optional iCol As Long = 3) As String
abc = a & CStr(iCol)
End Function
然后调用它:
Public Sub Test()
d = abc("aaa", 2)
MsgBox d
End Sub