You may at some point want to pass a variable to a procedure by value, even though the procedure indicates that the argument is passed by reference. You can do this by enclosing the variable to pass in parentheses. For example, the following AddThree procedure accepts arguments passed by reference. However, by enclosing the varToPass variable in parentheses in the calling procedure, you cause the variable to be passed by value. Therefore, in this example, the MsgBox function in TestPassingArgs will display the value 7 instead of the value 10.
Sub TestPassingArgs() varToPass = 7 AddThree (varToPass) MsgBox varToPass ' displays 7 End Sub Sub AddThree(ByRef passedVar As Integer) passedVar = passedVar + 3 End Sub
Note
Make sure that you don't accidentally pass a variable by value when you mean to pass it by reference. If you accidentally use parentheses around an argument when the syntax doesn't require it — that is, if you use parentheses around an argument to a procedure you call without the Call keyword, or around an argument to a Function procedure whose return value you're not using — you'll pass the variable by value. For more information about the syntax for calling procedures, see "Calling Procedures" earlier in this chapter.