Place a command button on your worksheet and add the following code lines:
Dim x As IntegerThe code calls the function Triple. It's the result of the second MsgBox we are interested in. Functions need to be placed into a module.
x = 10
MsgBox Triple(x)
MsgBox x
1. Open the Visual Basic Editor and click Insert, Module.
2. Add the following code lines:
Function Triple(ByRef x As Integer) As Integer
x = x * 3Result when you click the command button on the sheet:
Triple = x
End Function
3. Replace ByRef with ByVal.
Function Triple(ByVal x As Integer) As Integer
x = x * 3Result when you click the command button on the sheet:
Triple = x
End Function
Explanation: When passing arguments by reference we are referencing the original value. The value of x (the original value) is changed in the function. As a result the second MsgBox displays a value of 30. When passing arguments by value we are passing a copy to the function. The original value is not changed. As a result the second MsgBox displays a value of 10 (the original value)..