VBA variables and data types
A variable is a named place in memory. Your macro keeps values there so it can calculate with them later or write them somewhere else. Here you learn to declare variables with Dim, pick the right data type and let Option Explicit catch your typos.
In short: Dim total As Long declares a variable for whole numbers, total = Range("B2").Value fills it. Option Explicit at the very top of the module makes sure every variable has to be declared first.
What you need to know: Range and Cells, meaning how to read and write cells.
Try it right now
The import swapped first and last names. To swap them back you need somewhere to park one value; otherwise one name is gone before you have copied it.
Task The import mixed up first and last name: A2 holds the last name, B2 the first name. Swap the two values. To do that, keep one value in a variable that you declare with Dim … As String.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | First name | Last name | ||
| 2 | Morgan | Emily | e.morgan@example.com | |
| 3 | ||||
| 4 | ||||
| 5 |
How to declare a variable
Dim customer As String
Dim qty As Long
Dim price As Double
customer = Range("A2").Value
qty = Range("B2").Value
price = Range("C2").Value
Range("D2").Value = qty * price
Dim declares the variable: the name, then As and the data type. From then on it is available until the macro ends. The equals sign assigns a value, just as with cells: target on the left, value on the right. Names follow the same rules as macro names: start with a letter, no spaces. Case does not matter; the editor makes the spelling match everywhere.
The data types you need most
| Type | For | Range | Starts as |
|---|---|---|---|
Long | Whole numbers: quantities, row numbers | -2,147,483,648 to 2,147,483,647 | 0 |
Double | Decimals: prices, percentages | very large and very small numbers, about 15 digits of precision | 0 |
String | Text: names, codes | up to about 2 billion characters | "" (empty) |
Boolean | Yes/no: done, paid | True or False | False |
Date | Dates and times | January 1, 100 to December 31, 9999 | 0 |
Variant | Anything, when the type is open | whatever it holds | Empty |
Integer | Whole numbers, older type | -32,768 to 32,767 | 0 |
For everyday Excel work, Long, Double, String, Boolean and Date are enough. You will see Integer in older code and in many examples online. It stops at 32,767, and spreadsheets quickly have more rows or bigger totals than that. Use Long for whole numbers.
A date works like this: Dim dueDate As Date, then for example dueDate = Date. The Date function returns today's date. You can calculate with dates: dueDate + 14 is two weeks later. More in Dates.
One more exercise: fix an overflow
Two warehouses together hold more units than an Integer variable can take, and the macro stops. What needs to change?
Task Find the bug: the macro should write the total stock of both warehouses to B4, but it stops with "Run-time error '6': Overflow". Change the data type so that large stock levels fit.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Warehouse | Units | ||
| 2 | North warehouse | 21,400 | ||
| 3 | South warehouse | 17,850 | ||
| 4 | Total | |||
| 5 |
Option Explicit: catch typos at once
By default VBA lets you use a variable without declaring it with Dim first; it then gets the type Variant. That sounds convenient but is risky: a typo like discout instead of discount silently creates a new, empty variable. The macro runs, the result is wrong, and nothing points to the mistake.
With Option Explicit as the first line of the module, before any macros, every variable must be declared. An unknown name then causes "Compile error: Variable not defined" before a single line runs, and the editor highlights the spot.
Option Explicit
Sub DeliveryNote()
Dim discount As Double
discount = Range("B3").Value
Range("E3").Value = discout ' Compile error: Variable not defined
End Sub
Task Find the bug: the macro should copy customer, discount and delivery date from column B to the delivery note (E2:E4). Before it starts, VBA reports "Compile error: Variable not defined". Keep Option Explicit.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Order | Delivery note | |||
| 2 | Customer | Riverside Garden Centre | Customer | ||
| 3 | Discount | 5% | Discount | ||
| 4 | Delivery date | 14 Apr 2026 | Delivery date | ||
| 5 |
In real Excel
So you never forget Option Explicit, the VBA editor can add it to every new module: Tools > Options, Editor tab, tick Require Variable Declaration. It only affects modules you create after that; add the line to existing modules yourself. While a macro runs step by step (F8), the Locals window (View > Locals Window) shows every variable with its current value and type. In the exercises here you see the same under Step through.
Common mistakes
Integer too small
When an Integer variable goes past 32,767, the macro stops with "Run-time error '6': Overflow". That often happens months later, once the list has grown. Long saves you the trouble.
Text in a number variable
If the cell holds a word and the variable is a Long, VBA reports "Run-time error '13': Type mismatch". Check that you are reading the right cell. Whether a cell really holds a number can be tested with IsNumeric, which comes up together with If.
Several variables on one line
Dim qty, price As Double only makes price a Double; qty becomes a Variant. Each variable needs its own As: Dim qty As Long, price As Double.
Losing a value while swapping
Write Range("A2").Value = Range("B2").Value followed by Range("B2").Value = Range("A2").Value and you end up with the same value twice. Save the value first, then overwrite.
FAQ
Do I have to declare every variable?
Not without Option Explicit, but you should. Declared variables with a type make mistakes visible and the code easier to read.
When should I use Variant?
When a value can have different types, such as a cell that is sometimes empty and sometimes holds a number, or later for arrays taken from ranges. Otherwise choose the matching type.
How long does a variable keep its value?
A variable declared with Dim inside a macro lives only while the macro runs. The next run starts with the initial value again. If a value has to last, write it to a cell.
Can a variable be called A1?
Better not. Names like sales or lastRow tell you what is inside. A common style is lower case first and a capital for each new word.