Excel like a finance pro.
VBA · Variables
Set for objects
Object variables (ranges, sheets, workbooks) must be assigned with Set.
When to use it
Object variables (ranges, worksheets, workbooks, dictionaries) must be assigned with Set. Values (numbers, text, dates) are assigned with a plain equals sign.
The code
- Code
Dim ws As Worksheet Set ws = Worksheets("Data")
Worked examples
A sheet variable
Dim ws As Worksheet Set ws = Worksheets("Data") → ws now refers to the sheet
Then ws.Range("A1") and so on.
A range variable
Dim rng As Range Set rng = ws.Range("A1").CurrentRegion → A range variable
Reuse without retyping the address.
Release
Set ws = Nothing → Releases the reference
Optional at the end of a procedure.
Worth knowing
- Forgetting Set gives "Object variable not set" (error 91).
- Forgetting Set gives runtime error 91, "Object variable or With block variable not set".
- If rng Is Nothing tests whether an object was assigned.
- Set works with any object, including Application.
Where it goes wrong
- Writing rng = Range("A1") without Set assigns the value, not the range.
- Comparing objects uses Is, not =.
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.