Dim VBA – Declare Variable

The Dim Keyword in VBA

You can use the Dim keyword in order to declare a variable in VBA:

Dim n as Integer

Dim stands for Dimension. When you use the Dim keyword, state the name and data type of the new variable.

This is shown in the code below:

Sub DeclaringAVariableUsingDim()

Dim productName as String
Dim myNumber as Integer
Dim myRange as Range
Dim wb as Worksheet
Dim myValue as Boolean

End Sub

We have declared five different variables in the above code, and used five different data types. You can learn more about how to name variables and variable data types in our VBA Data Types – Variables and Constants tutorial where we go into detail about what variables are, how to name them and the different data types available in VBA.

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)