Excel like a finance pro.
VBA · Variables
Option Explicit
Forces every variable to be declared, so typos become errors instead of silent new variables.
When to use it
Option Explicit at the top of a module makes every variable require a Dim. Without it, a misspelled variable silently becomes a new empty one and the bug hides for hours.
The code
- Code
Option Explicit
Worked examples
Catch a typo
Option Explicit Sub Test() totl = 5 ' compile error: variable not defined End Sub → The typo is caught before the code runs
This is the whole point.
Turn it on for good
Tools > Options > Require Variable Declaration → Option Explicit is added to every new module
Set it once.
Compile check
Debug > Compile VBAProject → Every undeclared variable across the project is reported
Find them all.
Worth knowing
- Turn on Require Variable Declaration in Tools > Options so it is added to every module automatically.
- Add it to existing modules by hand; the option only affects new ones.
- Declaring types (As Long) also speeds code up.
- It must be the first line, before any procedure.
Where it goes wrong
- Old code without declarations will not compile until every variable is declared.
- Forgetting it is the most common source of "it worked yesterday" bugs.
Related
Learn the moves here — or let Wauvel run them on your numbers.
Meet your AI CFO →One CFO-grade Excel tip a week
A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.