Option Explicit
Forces every variable to be declared before use.
Option ExplicitThis static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Forces every variable to be declared before use.
Option Explicit is one of the highest-value safety lines in VBA because typo-driven implicit Variants are a common source of silent bugs in workbook automation. It catches naming mistakes at compile time and makes later debugging much faster. Use it by default in every module.
Quick reference
Syntax
Option Explicit
See it in practice
Examples
Catch a misspelled variable early
Option Explicit
Sub Example()
Dim lastRow As Long
lastRwo = 10
End SubWith Option Explicit, the typo is caught before the macro runs on real workbook data.
Debug faster
Common Errors
Compile error
Cause: Turning on Option Explicit in a module with undeclared legacy variables.
Fix: Declare each variable properly instead of disabling the directive.
Runtime support
Compatibility
Common questions
Frequently Asked Questions
Forces every variable to be declared before use.
Compile error: Declare each variable properly instead of disabling the directive.