ByVal or ByRef - Collections

The Collection data type is a Reference data type.
When you declare a variable with a Collection data type it can be passed in using either ByRef or ByVal.
In VBA all reference data types are passed by reference although there is a subtle difference.
Although it is a Reference data type there is a difference between passing ByRef vs ByVal


ByRef

the address of the object is passed.
You must use the Set statement because VBA.Collection is a reference data type.
The second Debug.Print displays the number 0.


ByVal

a copy of the address to the object is passed.
You must use the Set statement because VBA.Collection is a reference data type.
The second Debug.Print displays the number 4.



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