Skip to the stories
Excel & Power BI

VBA Variable Declarations: Use Option Explicit and Choose the Right Data Type

Declare VBA variables with Option Explicit, suitable data types and clear names to catch typos early and make Excel macros easier to maintain.

VBA Variable Declarations: Use Option Explicit and Choose the Right Data Type

Undeclared variables can turn a small VBA typo into a result that looks valid but is wrong. In Excel macros, a misspelled variable name may be treated as a second, empty variable, so the procedure can continue without flagging the mistake. Declaring variables and enabling Option Explicit makes that class of error easier to catch.

Catch misspelled names with Option Explicit

Consider a procedure that adds five to a total. The second assignment contains a typo: it refers to MyTotl instead of MyTotal.

Sub CalculateTotal()
    MyTotal = 10
    MyTotal = MyTotl + 5
    MsgBox MyTotal
End Sub

Without a declaration requirement, VBA can create MyTotl as a new, empty variable. The calculation then uses that variable rather than the value stored in MyTotal. In this example, the displayed result is 5 instead of 15. In a longer macro, the mistake may be harder to trace because the procedure runs instead of stopping at the typo.

Place Option Explicit at the top of a module to require variables to be declared before use. With it enabled, VBA flags an undeclared name, prompting you to correct the spelling or add the intended declaration.

Option Explicit

Sub CalculateTotal()
    Dim MyTotal As Long
    MyTotal = 10
    MyTotal = MyTotal + 5
    MsgBox MyTotal
End Sub

To have new modules include this setting automatically, open the VBA editor’s Tools menu, choose Options, and select Require Variable Declaration. This makes the check part of the editor’s default setup for new modules.

image excel exercises

Declare a type that matches the value

A declaration uses Dim, the variable name, and As followed by a data type. For example:

Dim MyTotal As Long

Dim is short for “Dimension.” The declaration tells VBA what kind of value the variable is intended to hold. Choosing an appropriate type makes the purpose of the variable clearer and helps prevent a mismatch between the data and the operation that uses it.

Four common types cover many everyday spreadsheet tasks:

ValueVBA typeTypical use
Whole numbersLongRow numbers or quantities such as 1, 25 or 1000
Decimal numbersDoublePrices or rates such as 12.5 or 0.2
TextStringA name or city such as “Paris”
DatesDateA delivery date, such as 11/08/2026

Those choices can be made explicit in the code:

image excel quizzes
Dim MyRow As Long
Dim MyPrice As Double
Dim MyCity As String
Dim MyDate As Date

The data type should reflect what the variable represents and how the macro will use it. A worksheet row number, for example, is a whole-number value. Declaring it as a decimal type suggests that values such as 1.5 might be valid, even though a worksheet has no half-row.

Keep worksheet positions as whole numbers

This procedure stores a row position in a Double and assigns it 1.5:

Sub WriteInRow()
    Dim MyRow As Double
    MyRow = 1.5
    Cells(MyRow, 1) = "Hello"
End Sub

The code then passes that value to Cells as a row index. A worksheet row is identified by a whole number, so a fractional row position doesn't describe a valid destination. The example illustrates why the type should match the value’s role: use Long for a row number, rather than allowing a decimal value that the task cannot use.

A type declaration helps set expectations, but it doesn't replace checking the values your macro receives. If a row number comes from a cell or another input, the procedure still needs to handle that input appropriately for its task.

Use names that explain the code

Clear names reduce the effort required to understand a procedure later. Names such as MyRow, MyColumn and MyWorkbook describe what each variable represents. That context is especially useful when a macro has several variables with different roles.

Single-letter names such as x and n offer little help to someone reading unfamiliar code. The common exception is i, often used as an index or loop counter. It is a familiar convention, and it can be declared like any other variable:

Dim i As Long

For variables that aren't loop counters, a descriptive name usually makes the code easier to follow. A few extra characters are a small trade-off for being able to tell what a value represents without searching through the whole procedure.

Use editor completion for longer names

Descriptive names don't have to mean repeatedly typing the full name. In the VBA editor, enter the first characters of a name and press Ctrl + Space. The editor can show matching names or complete the name directly.

That shortcut makes it easier to keep names consistent as a procedure grows. Combined with Option Explicit and suitable data types, clear naming gives VBA code a useful baseline: undeclared names are flagged, values have an intended type, and readers can see what each variable is for.

Source: Frédéric LE GUEN · excel-tutorial.com

Same flag