MOCKSTACKS
EN
Questions And Answers

More Tutorials









VBA Passing Arguments ByRef or ByVal

Passing Simple Variables ByRef And ByVal


Passing ByRef or ByVal indicates whether the actual value of an argument is passed to the CalledProcedure by the CallingProcedure, or whether a reference (called a pointer in some other languages) is passed to the CalledProcedure.

If an argument is passed ByRef, the memory address of the argument is passed to the CalledProcedure and any modification to that parameter by the CalledProcedure is made to the value in the CallingProcedure.

If an argument is passed ByVal, the actual value, not a reference to the variable, is passed to the CalledProcedure.

A simple example will illustrate this clearly:

Sub CalledProcedure(ByRef X As Long, ByVal Y As Long)
 X = 321
 Y = 654
End Sub
Sub CallingProcedure()
 Dim A As Long
 Dim B As Long
 A = 123
 B = 456
 Debug.Print "BEFORE CALL => A: " & CStr(A), "B: " & CStr(B)
 ''Result : BEFORE CALL => A: 123 B: 456
 CalledProcedure X:=A, Y:=B
 Debug.Print "AFTER CALL = A: " & CStr(A), "B: " & CStr(B)
 ''Result : AFTER CALL => A: 321 B: 456
End Sub

Another example:

Sub Main()
 Dim IntVarByVal As Integer
 Dim IntVarByRef As Integer

 IntVarByVal = 5
 IntVarByRef = 10

 SubChangeArguments IntVarByVal, IntVarByRef '5 goes in as a "copy". 10 goes in as a reference
 Debug.Print "IntVarByVal: " & IntVarByVal 'prints 5 (no change made by SubChangeArguments)
 Debug.Print "IntVarByRef: " & IntVarByRef 'prints 99 (the variable was changed in
SubChangeArguments)
End Sub
Sub SubChangeArguments(ByVal ParameterByVal As Integer, ByRef ParameterByRef As Integer)
 ParameterByVal = ParameterByVal + 2 ' 5 + 2 = 7 (changed only inside this Sub)
 ParameterByRef = ParameterByRef + 89 ' 10 + 89 = 99 (changes the IntVarByRef itself - in the
Main Sub)
End Sub


Conclusion

In this page (written and validated by ) you learned about VBA Passing Arguments ByRef or ByVal . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Scripting FileSystemObject.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.