I have stepped in through this code several times to see the difference between By Val, By Ref:
Code: Select all
Sub Test()
Dim x As Long
' Pass by value - x will not change
x = 1
Debug.Print "x before ByVal is"; x
SubByVal x
Debug.Print "x after ByVal is"; x
' Pass by reference - x will change
x = 1
Debug.Print "x before ByRef is"; x
SubByRef x
Debug.Print "x after ByRef is"; x
End Sub
Sub SubByVal(ByVal x As Long)
' x WILL NOT change outside as passed ByVal
x = 99
End Sub
Sub SubByRef(ByRef x As Long)
' x WILL change outside as passed ByRef
x = 99
End Sub
I can see By val the value is not chaned, when I step through it, I can't find it why. Can we say it is because machine/ computer is set this way?
Regards,
BittenApple