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 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.
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
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.
For example, do this.
Sub Sample()
Dim s1 As String, s2 As String
s1 = "Blah": s2 = "Blah Blah"
callByValue s1, s2
End Sub
This is better definition for a ByVal call 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With