Ad space (banner)
Lesson 17 / 20

Arguments (ByVal and ByRef)

This lesson covers the difference between ByVal and ByRef, the two ways of passing an argument.

ByVal passes a copy of the value, so changing it inside the procedure does not affect the caller's variable. ByRef passes the variable itself, so a change inside is reflected back at the caller. VBA defaults to ByRef when you specify neither.

The sample code doubles the value inside DoubleValue(ByRef num As Long) and confirms that the caller's variable changed too.

A common early stumble is not realising the default is ByRef, and unintentionally changing the caller's variable. Marking arguments you do not want changed as ByVal explicitly is the safe habit.

In professional work, many coding standards require writing ByVal or ByRef explicitly on every argument, to avoid unintended side effects.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VBA directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)