ByRef (keyword)


...,ByRef parameter,...


Used within the Sub...End Sub, Function...End Function, or Declare statement to specify that a given parameter can be modified by the called routine.


Passing a parameter by reference means that the caller can modify that variable's value.

Unlike the ByVal keyword, the ByRef keyword cannot be used when passing a parameter. The absence of the ByVal keyword is sufficient to force a parameter to be passed by reference:

  MySub ByVal I      '<-- Pass i by value.

  MySub ByRef i      '<-- Illegal (will not compile).
  MySub i            '<-- Pass i by reference.


Sub Test(ByRef a As Variant)

  a = 14
End Sub

Sub Main()

  b = 12
  Test b
  MsgBox "The ByRef value is: " & b      '  <-- Displays 14.
End Sub

See Also

() (keyword), ByVal (keyword).

More information