I have noticed an oddity when passing argument ByRef in a function when not using the return value. Take this example:
Option Explicit
Function Triple(ByRef x As Integer) As Integer
x = x * 3
Triple = x
End Function
Sub test()
Dim x As Integer
Dim y As Integer
x = 5
y = Triple(x)
Debug.Print x
End Sub
It print 15, as expected. Now remove the assignement when calling Triple:
Option Explicit
Function Triple(ByRef x As Integer) As Integer
x = x * 3
Triple = x
End Function
Sub test()
Dim x As Integer
Dim y As Integer
x = 5
Triple(x)
Debug.Print x
End Sub
To my surprise, it prints 5. Why this behavior? Is it documented somewhere? (I am on Excel 2016).
Bookmarks