
📎 This article includes 1 downloadable practice file ↓
📘 Excel VBA Course · Lesson 1 of 15 — see all lessons
In this article
A variable is a named box where your macro keeps a value while it runs — a total, a customer name, a row number. Getting variables right is the difference between a macro that works once and one that keeps working.
Declare every variable with Dim
Dim customerName As String
Dim invoiceTotal As Double
Dim rowNum As Long
Dim isPaid As Boolean
Dim reserves the box and As says what may go in it. You can declare several on one line, but each needs its own type: Dim a As Long, b As Long. Writing Dim a, b As Long makes only b a Long — a becomes a Variant.
Turn on Option Explicit
Put Option Explicit at the very top of every module (or tick Tools → Options → Require Variable Declaration so the editor adds it for you). Without it, a typo like invoiceTotl silently creates a new empty variable and your total ends up as 0.
Which data type should I use?
| Type | Holds | Use it for |
|---|---|---|
| Long | Whole numbers up to about ±2.1 billion | Row numbers, counters, IDs |
| Double | Decimal numbers | Amounts, rates, measurements |
| Currency | Fixed 4-decimal numbers | Money where rounding must be exact |
| String | Text | Names, codes, file paths |
| Boolean | True / False | Flags like isPaid |
| Date | Date and time | Invoice dates, timestamps |
| Variant | Anything | Reading a whole range into an array |
| Object types | Worksheet, Range, Workbook… | References to Excel objects (need Set) |
Objects need Set
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sales")
ws.Range("A1").Value = "Updated " & Now
Forgetting Set gives “Object variable or With block variable not set”. It is one of the bugs in the VBA Bug Hunt.
Scope: where a variable lives
- Inside a Sub (
Dim) — exists only while that procedure runs. - Top of a module (
Private) — shared by all procedures in that module. - Top of a module (
Public) — visible to the whole project. Use sparingly; global state makes bugs hard to trace.
Constants for values that never change
Const VAT_RATE As Double = 0.18
Const REPORT_SHEET As String = "Report"
Change a rule in one place instead of hunting for “0.18” across twenty procedures.
A worked example: swap two cells
Sub SwapAB()
Dim temp As Variant
temp = Range("A1").Value
Range("A1").Value = Range("B1").Value
Range("B1").Value = temp
End Sub
The temporary variable holds A1 so it is not lost when A1 is overwritten. Step through it line by line in the VBA Playground.
Naming tips
- Describe the content:
lastRow,customerCount— notxortemp2. - No spaces; start with a letter; avoid VBA keywords like
NameorDate.
Try it yourself: step by step
- Download vba-course-practice.xlsm and open it. Click Enable Content in the yellow bar.
- Go to the Macros sheet — there is a button for every lesson. Press Alt+F11 any time to read the code.
- Type 10 in A1 and 25 in B1 of any sheet, then run
L01_SwapABwith Alt+F8. The values swap. - Put the cursor inside the macro and press F8 repeatedly to step through it; hover over
tempto see its value. - Change
Dim temp As VarianttoDim temp As Longand swap two text values — see the “Type mismatch” error, then change it back.
📎 Practice files for this article
- 📗Practice workbook with all course macros (.xlsm)Open it, click Enable Content in the yellow bar, go to the Macros sheet and press any button. 60 sales rows included to test on.⬇ XLSM · 29 KB
Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.