TL;DR — Put
Option Explicitat the very top of every module and VBA will refuse to run any macro that uses a variable you never declared. Without it, a typo liketotalSaiesis not an error — VBA silently creates a new empty variable, your total comes back0, and nothing warns you. It is a directive, not runtime code: it runs at compile time, so the mistake surfaces before a single line executes. Turn on Tools > Options > Require Variable Declaration once so it is added to every new module automatically.
Option Explicit ' <- always the first line of the module
Sub Report()
Dim totalSales As Long
totalSales = 100
MsgBox totalSaies ' typo! With Option Explicit: "Variable not defined" - it will not run
End Sub ' Without it: MsgBox shows a blank/0 and no one ever knows
What you'll learn
- The mental model — VBA invents a variable for every name you type
- The silent-typo bug, and how one line kills the whole class of it
- Why it is per-module, and the setting that adds it for you
- What to do when Variable not defined lights up your old code
- Why it forces declaration but not data types (a common mix-up)
- The opinion: the one setting to turn on before you write a line
The mental model: VBA invents variables behind your back
By default, VBA treats any name it has never seen as a request to create a new variable, on the spot,
of type Variant, holding Empty. That sounds convenient. It is a trap. It means VBA cannot tell the
difference between a variable you meant to use and a variable you misspelled — both are just "a name it
has not seen," so both get created, silently.
Option Explicit removes that convenience. With it at the top of the module, the rule flips: every
variable must be declared (with Dim, Private, Public, Static, Const, or a parameter) before you
use it. Type a name you never declared and the compiler stops with Variable not defined — before the
macro runs. You have traded a little typing for the compiler's promise that every name in your code is one
you meant.
Three tokens the macro never runs
Option Explicit belongs to a small family: three things you type that the running macro never executes.
They shape the source and the compile, not the run — which is why beginners skip them and why
skipping them hurts.
| Token | What it does to your source | What it costs the run |
|---|---|---|
' comment |
Leaves notes and disables code the compiler deletes | Nothing — it never runs |
_ line-continuation |
Splits one long statement across many lines | Nothing — the parser erases it |
Option Explicit |
Forces every name to be declared | Runs at compile time, so typos die before the macro starts |
Two of the three are erased before compiling; Option Explicit is the one that acts — it instructs the
compiler to police your names. It is the highest-leverage of the three, because the bug it catches is the
most expensive kind: the one that produces a wrong answer instead of an error.
The bug it kills: the typo that returns zero
This is the whole case for Option Explicit in eight lines. Without it:
Sub Totals()
Dim revenue As Currency
revenue = 5000
' ...forty lines later...
MsgBox "Revenue: " & revanue ' typo: revAnue
End Sub
revanue was never declared, so VBA creates it as an empty Variant. The message box reads
Revenue: — blank — and there is no error, no warning, no red text. On a real report that is a wrong
number shipped to someone who trusts it. This is the single most common source of "my macro gives the
wrong answer but does not crash," and it is entirely invisible until someone notices the total is off.
Add one line and the same typo becomes a wall you cannot walk past:
Option Explicit
Sub Totals()
Dim revenue As Currency
revenue = 5000
MsgBox "Revenue: " & revanue ' Compile error: Variable not defined (revanue highlighted)
End Sub
The macro will not even start. The compiler highlights revanue and names the problem. A ten-second fix
instead of a silent, shipped mistake.
It is per-module, so let the setting add it for you
Option Explicit protects only the module it sits in. Add it to Module1 and Module2 is still
wide open. A new module you insert tomorrow has none. Relying on yourself to type it at the top of every
module is exactly the kind of discipline that fails on the busy day you most need it.
So do not rely on yourself. In the VBA editor open Tools > Options > Editor and tick Require Variable
Declaration. From then on, every new module you insert gets Option Explicit on its first line
automatically. It is a one-time click that makes the safe default the actual default — the first thing to
set on any machine you write VBA on.
One caveat worth knowing: that setting only affects modules created after you tick it. Modules that already exist keep whatever they had, so you still add the line to old modules by hand.
Turning it on in old code: a wall of red is the point
Add Option Explicit to a module full of undeclared variables and the first compile lights up like a
switchboard — Variable not defined on name after name. That is not the directive being difficult; that
is it showing you every place your old code was flying blind. Each highlighted name is either a variable
you need to declare or a typo you never caught.
Work through them one at a time: declare the real ones (Dim customerName As String — see
VBA Dim for how), and fix the typos. It is tedious once and safe forever. If a module is
enormous, add the line, compile, and let the editor walk you to each undeclared name with Debug >
Compile. You are not adding work; you are cashing in bugs that were already there.
It forces declaration, not data types
A frequent misread: people think Option Explicit makes VBA strongly typed. It does not. It forces you
to declare a variable, but a declaration with no type is still a Variant:
Option Explicit
Dim count ' legal under Option Explicit - but it is a Variant
Dim count As Long ' this is what you actually want - a typed variable
Dim count satisfies Option Explicit (the name is declared) yet gives you none of the speed or
type-safety of As Long. Option Explicit closes the undeclared-name hole; choosing real types
(As Long, As String, As Range) is a separate, equally worthwhile habit covered in
VBA Data Types. Turn on the first, then practice the second.
The opinion: this is the one setting to turn on first
There is no honest argument against Option Explicit. The cost is a few Dim lines you should be writing
anyway; the return is immunity to the single most time-wasting bug in VBA — the silent typo that returns
zero and ships. Every experienced VBA developer turns on Require Variable Declaration on a new machine
before they write anything, and treats a module without Option Explicit as unfinished.
If you take one thing from this cluster, make it this: tick the setting now, add the line to the modules you already have, and never again spend an afternoon hunting a wrong total that turns out to be a misspelled variable. It is the cheapest insurance in the language.
When the guarantee you want is the whole macro, not one variable
Option Explicit guarantees your names are real. It cannot guarantee your logic is. When the stakes
are a client-facing number, you still want something checking that the output is right, not just that the
variables exist. ExcelMaster takes
a plain-English goal — "reconcile these two sheets and flag every row where the totals disagree" — writes
the code, backs up your workbook, runs it, and hands back the rows that fail the check. You describe the
result you expect; it does the work and shows you where reality and expectation part ways — a level of
safety no single directive can give you.
Frequently asked questions
What does Option Explicit do in VBA?
It forces you to declare every variable before using it. With Option Explicit at the top of a module,
any name you never declared triggers a Variable not defined compile error, so typos and undeclared
variables are caught before the macro runs instead of silently becoming empty variants.
Where do I put Option Explicit?
On the very first line of a module, above every Sub and Function, in the module's Declarations
section. It applies only to that module, so it goes at the top of each module you want protected.
How do I add Option Explicit automatically?
In the VBA editor, open Tools > Options > Editor and tick Require Variable Declaration. VBA then
inserts Option Explicit at the top of every new module you create. It does not add it to modules that
already exist — those you update by hand.
Why am I getting Variable not defined after adding Option Explicit?
Because the module has variables you never declared — often typos, sometimes names you just forgot to
Dim. The compiler highlights each one. Declare the real variables (for example Dim total As Long) and
correct the misspelled ones; each error is a latent bug the directive just exposed.
Does Option Explicit force data types?
No. It forces declaration, not typing. Dim count satisfies it but is still a Variant. To get type
safety and speed you add a type yourself — Dim count As Long — which is a separate good habit.
Tested in
Tested in: Excel 365 (Windows 11), VBA 7.1 — last verified 2026-09-06.
Related guides: VBA Comment · VBA Line Continuation · VBA Dim · VBA Data Types · VBA Debug.Print
