VbaModulesBeginner

Option Explicit

Forces every variable to be declared before use.

Review the syntaxStudy the examplesOpen the coding app
Option Explicit

This 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

1

Catch a misspelled variable early

Option Explicit

Sub Example()
    Dim lastRow As Long
    lastRwo = 10
End Sub

With Option Explicit, the typo is caught before the macro runs on real workbook data.

Debug faster

Common Errors

1

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

Excel desktop VBA

Source: Microsoft Learn Office VBA reference

Common questions

Frequently Asked Questions

Forces every variable to be declared before use.

Compile error: Declare each variable properly instead of disabling the directive.