Excel like a finance pro.
VBA · Variables
Declaring variables with Dim
Declares a variable and its type before use.
When to use it
Dim declares a variable and its type before it is used. Typed variables are faster, catch mistakes, and get autocomplete for object members.
The code
- Code
Dim total As Double Dim name As String Dim i As Long
Worked examples
Basic declarations
Dim total As Double Dim name As String Dim i As Long → Three typed variables
One per line is clearest.
A classic trap
Dim a, b As Long → a is Variant, only b is Long
Each variable needs its own As.
Object variable
Dim ws As Worksheet Set ws = Worksheets("Data") → An object variable, assigned with Set
Objects need Set.
Worth knowing
- Declare at the top of the procedure so the reader sees every variable at once.
- Long for whole numbers, Double for decimals, String for text, Boolean for flags.
- Use Option Explicit so a missing Dim is an error.
Where it goes wrong
- Dim a, b As Long declares a as Variant.
- A variable declared without As is a Variant, slower and loosely typed.
Related
Learn the moves here — or let Wauvel run them on your numbers.
Meet your AI CFO →One CFO-grade Excel tip a week
A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.