- 01Setting up (what you'll need)
- 02Your first output (MsgBox)
- 03Variables (Dim)
- 04Branching (the If statement)
- 05Repeating (the For statement)
- 06Repeating (the Do While statement)
- 07Writing a Sub procedure
- 08Function procedures (work that returns a value)
- 09Reading and writing cells (Range and Cells)
- 10Working with arrays
- 11Working with strings
- 12The With statement
- 13Object variables and working with worksheets
- 14Looping over cells (For Each)
- 15Error handling (On Error)
- 16The Select Case statement
- 17Arguments (ByVal and ByRef)
- 18Working with collections (Collection)
- 19Recording macros and event procedures
- 20[Project] Build a small inventory sheet
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.
๐งช 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).
