ByVal or ByRef


ByRef (Default)

Passing variables by reference is the default and this means that you pass a pointer to the variable and therefore the variable's value can be changed inside the procedure or function.
Passing an argument by reference is the default.

Sub Proc_Name(Name As String) 
End Sub

Sub Proc_Name(ByRef Name As String)
End Sub

If you pass a variable defined as a user defined data type to a procedure, it must be passed by reference.
Attempting to pass it by value will cause an error.


ByVal

Passing arguments by value passes a copy of the variable and changing the value will only affect the local copy.
This should be used when you do not want the procedure to modify the actual variable but just to use the value.

Sub Proc_Name(ByVal Name As String) 
End Sub

Passing in Value Data Types

Value data types are also referred to as simple data types.


Passing in Reference Data Types

Reference data types are also referred to as object types.
In VBA all objects are passed by reference although there is a difference.
ByRef - the address of the object is passed.
ByVal - a copy of the address to the object is passed.


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext