This question already has an answer here:
这个问题在这里已有答案:
- VBA returning error when calling a Sub with multiple parameters 1 answer
- 调用具有多个参数的Sub时,VBA返回错误1回答
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时,它会抛出一个编译器错误。
2 个解决方案
#1
14
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只有s2会被声明为String而第一个将被声明为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.
我注意到的另一件事是你在调用sub之前没有分配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
#2
2
This is better definition for a ByVal call sub.
这是ByVal调用sub的更好定义。
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
#1
14
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只有s2会被声明为String而第一个将被声明为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.
我注意到的另一件事是你在调用sub之前没有分配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
#2
2
This is better definition for a ByVal call sub.
这是ByVal调用sub的更好定义。
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