Description
Used at module level to force explicit declaration of all variables in that module.
Syntax
Option Explicit
Remarks
If used, the Option Explicit statement must appear in a module before any statements that declare variables or define constants.
If you don't use the Option Explicit statement, all undeclared variables are Variant unless the default type is otherwise specified with a Deftype statement.
When you use the Option Explicit statement, you must explicitly declare all variables using the Dim, Private, Public, ReDim, or Static statements. If you attempt to use an undeclared variable name, an error occurs at compile time.
Tip
Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid risking confusion in code where the scope of the variable is not clear.
See Also
Const Statement, Deftype Statements, Dim Statement, Function Statement, Option Base Statement, Option Compare Statement, Option Private Statement, Private Statement, Public Statement, ReDim Statement, Static Statement, Sub Statement.
Example
This example uses the Option Explicit statement to force you to explicitly declare all variables. Attempting to use an undeclared variable gives you an error at compile time. The Option Explicit statement is used at the module-level only.
Option Explicit ' Force explicit variable declaration. Dim MyVar ' Declare variable. MyInt = 10 ' Undeclared variable generates error. MyVar = 10 ' Will not generate error.