如何将多个参数传递给 VBA 中的过程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20285505/
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
How to pass multiple arguments to procedure in VBA?
提问by Samraan
I am trying to pass two string parameters to a Sub, but its not allowing me do that. The approach I have used is given below.
我试图将两个字符串参数传递给 Sub,但它不允许我这样做。我使用的方法如下。
Sub callByValue( str1 As String, str2 As String)
MsgBox str1
MsgBox str2
End Sub
Calling Macros:
调用宏:
Dim s1, s2 As String
callByValue(s1,s2)
While calling callByvalue
, it's throwing a compiler error.
调用时callByvalue
,它会引发编译器错误。
回答by Siddharth Rout
You need to remove the brackets
您需要删除括号
callByValue s1, s2
Also remember in VBA, When you say Dim s1, s2 As String
only s2
will be declared as String
and the first one will be declared as Variant
还要记住在 VBA 中,当您说Dim s1, s2 As String
only s2
will beclared asString
并且第一个将被声明为Variant
One more thing that I noticed was that you didn't assign the values of s1
and s2
before calling the sub. You will get a blank for both.
还有一两件事,我注意到的是,你没有指定的值s1
,并s2
调用子之前。您将获得两者的空白。
For example, do this.
例如,执行此操作。
Sub Sample()
Dim s1 As String, s2 As String
s1 = "Blah": s2 = "Blah Blah"
callByValue s1, s2
End Sub
回答by jacouh
This is better definition for a ByVal call sub.
这是对 ByVal 调用子的更好定义。
Sub callByValue(ByVal str1 As String, ByVal str2 As String)
MsgBox str1
MsgBox str2
End Sub
Sub sof20285505callByVal()
Dim s1, s2 As String
callByValue s1, s2
End Sub