i was playing with some VBA when i noticed this (for me) strange behaviour. I know that by default, Excel VBA passes arguments by reference, so i would expect from both 'ways of calling' the sub TestSub
the same result. Why is that when i call it with parenthesis it behaves like the argument was passed ByVal
?
My expectations:
TestSub (intSomeNumber) 'prints 15
TestSub intSomeNumber 'prints 15
Reality:
TestSub (intSomeNumber) 'prints 10
TestSub intSomeNumber 'prints 15
Here is the code:
Sub Macro_1()
Dim intSomeNumber As Integer: intSomeNumber = 10
' If i'll call TestSub without parenthesis
' the value of intSomeNumber will be changed to 15 as expected
TestSub intSomeNumber
' If i'll call TestSub with parenthesis
' the value won't change, so the result printed via Debug.Print will be 10
'TestSub (intSomeNumber)
Debug.Print CStr(intSomeNumber)
End Sub
Private Sub TestSub(argIntSomeNumber As Integer)
argIntSomeNumber = 15
End Sub