Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple arguments to procedure in VBA? [duplicate]

Tags:

excel

vba

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.

like image 575
Samraan Avatar asked Nov 29 '13 12:11

Samraan


2 Answers

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
like image 89
Siddharth Rout Avatar answered Sep 30 '22 14:09

Siddharth Rout


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
like image 43
jacouh Avatar answered Sep 30 '22 13:09

jacouh