🚀The world's best VBA AI has evolved. ExcelMaster is now an autonomous Agent.Read more →
Back to Blog

VBA New in Excel — Creating Objects, New vs CreateObject, and the Dim As New Trap

|

VBA New in Excel — Creating Objects, New vs CreateObject, and the Dim As New Trap

TL;DRNew brings an object into existence: Set d = New Dictionary builds a fresh, empty Dictionary and points d at it. New is early binding — it needs a library reference but gives you IntelliSense, compile-time checks and speed. CreateObject is late binding — no reference, resolved at run time, portable across machines. And avoid Dim d As New Dictionary: it creates the object lazily and quietly resurrects it after you release it. Prefer Dim d As Dictionary then Set d = New Dictionary.

Sub CreateAnInstance()
    ' Early binding - Tools > References > Microsoft Scripting Runtime
    Dim d As Dictionary
    Set d = New Dictionary                        ' New builds a brand-new, empty Dictionary
    d.Add "a", 1

    ' Late binding - no reference, resolved at run time, portable
    Dim late As Object
    Set late = CreateObject("Scripting.Dictionary")
End Sub

New is the "birth" step of the object lifecycle: New makes the object, Set points a variable at it, Nothing releases it. Where Set decides which object a label points at, New decides whether a brand-new one comes into existence at all.

What you'll learn

  • The mental model — New is the constructor, the moment an object is born
  • New versus CreateObject — early binding versus late binding, and when each wins
  • The Dim As New trap — lazy creation and an object that resurrects itself after Nothing
  • Why one New means one instance, and what New in a loop does
  • What you can and cannot New (your class modules and Dictionary, yes; a Worksheet, no)

The mental model: New is the moment of birth

A class or a library gives you a blueprint; New stamps out one physical instance from it. Set d = New Dictionary is two steps written on one line: New Dictionary builds a new, empty dictionary in memory, and Set d = points the variable d at it. Read it right to left — the object is created first, then the label is attached.

That is the difference between New and Set alone. Set ws = Worksheets("Data") points a label at a sheet that already exists; nothing new is created. Set d = New Dictionary creates the object before pointing at it. If you want a fresh, empty object, you need New (or CreateObject); if you want to refer to one that already exists, you just need Set.

New versus CreateObject: early versus late binding

There are two ways to make an external object, and the choice is really the choice between early and late binding:

' Early binding - New - needs the reference ticked, gives IntelliSense + compile checks
Dim d As Dictionary
Set d = New Dictionary

' Late binding - CreateObject - no reference, resolved at run time, portable
Dim d As Object
Set d = CreateObject("Scripting.Dictionary")

New (early binding) requires you to add the library under Tools ▸ References (for a Dictionary, Microsoft Scripting Runtime). In return you get IntelliSense as you type, the compiler catches a misspelled method before you run, and calls are a touch faster. The cost: if you ship the workbook to a machine where that reference is missing or a different version, it can break with Can't find project or library.

CreateObject (late binding) needs no reference at all — you declare the variable As Object and name the class as a string, and VBA resolves it at run time. That makes the workbook portable across versions and machines, at the price of no IntelliSense and slightly slower calls. The rule of thumb: New for code that stays on machines you control and want tooling for; CreateObject for code you distribute widely.

The Dim As New trap

Dim d As New Dictionary looks like a tidy shortcut — declare and create in one line. It is the one form to avoid, for three reasons that all trace back to the same behavior: VBA does not create the object at the Dim; it creates it lazily, on first use, and re-checks on every use.

Dim d As New Dictionary        ' looks convenient - but the object is not made yet
' ... later ...
d.Add "a", 1                   ' NOW VBA quietly creates it, on first touch
  1. It is created lazily, with a hidden check on every access. Behind each use of d, VBA inserts an invisible "if d is Nothing, create it" test. Small, but it runs on every single reference.
  2. You cannot reliably test Is Nothing. The moment you write If d Is Nothing, the act of touching d triggers that auto-create, so the test is effectively always False. You lose the ability to ask "was this ever set?".
  3. It resurrects itself after you release it. Set d = Nothing seems to release the object, but the very next time you touch d, VBA makes a new empty one. You can never truly let it go.

The fix is to separate declaration from creation, which is also clearer to read:

Dim d As Dictionary            ' declare the type - d starts as Nothing
Set d = New Dictionary         ' create it exactly when you mean to

One New is one instance

New creates one object each time it runs. That matters most in loops. If you need a fresh object per iteration — a new class instance per row, say — put the New inside the loop:

Dim i As Long, item As clsLineItem
For i = 1 To 100
    Set item = New clsLineItem     ' a brand-new object each pass
    item.Load rows(i)
    log.Add item
Next i

Each pass builds a new clsLineItem and the previous one lives on inside log. If instead you New once before the loop and reuse the same object, every entry in log ends up pointing at the same instance holding the last row's data — a classic "all my records are identical" bug.

What you can and cannot New

You can New your own class modules, and creatable library classes such as Dictionary (with the reference) and the intrinsic Collection (New Collection needs no reference at all — it is built into VBA). What you cannot New are Excel's own model objects: there is no New Worksheet or New Workbook. Those are created through the object model — Worksheets.Add, Workbooks.Add — because Excel owns their lifetime, not you. Trying Set ws = New Worksheet is a compile error, not a runtime one.

How ExcelMaster helps

Creating an object has more forks than it looks: New or CreateObject, early or late binding, a reference to tick or not, and the Dim As New shortcut that quietly breaks release and Is Nothing.

ExcelMaster picks the right one from what you describe — late binding with CreateObject when you say the macro has to run on other people's machines, early binding with New and the reference noted when you want speed and tooling — and it always separates Dim d As Dictionary from Set d = New Dictionary, so lazy creation and self-resurrection never bite you. You keep the workbook and the code.

Frequently asked questions

What does New do in VBA?

New creates a brand-new instance of an object from its class or library blueprint. Set d = New Dictionary builds a fresh, empty Dictionary in memory and points d at it. Use New when you want a new object; use plain Set (without New) when you want to refer to an object that already exists.

Should I use New or CreateObject?

Use New (early binding) when the code stays on machines you control and you want IntelliSense, compile checks and speed — it needs the library ticked under Tools ▸ References. Use CreateObject (late binding) for code you distribute, because it needs no reference and is portable across versions, at the cost of IntelliSense.

What is wrong with Dim As New?

Dim d As New Dictionary creates the object lazily on first use, inserts a hidden Nothing-check before every access, makes If d Is Nothing effectively always False, and resurrects a new object the next time you touch it after Set d = Nothing. Prefer Dim d As Dictionary followed by Set d = New Dictionary so creation and release are explicit.

Can I use New with a Worksheet or Range?

No. Excel owns the lifetime of Worksheet, Workbook, Range and similar model objects, so there is no New Worksheet. Create them through the object model instead — Worksheets.Add, Workbooks.Add — and use New only for your own class modules and creatable library classes such as Dictionary and Collection.

Do I need Set with New?

Yes, for object variables: Set d = New Dictionary. New builds the object and Set points the variable at it. The only form that omits Set is Dim d As New Dictionary, which we recommend avoiding for the reasons above.

Tested in

Tested in: Excel 365 (Windows 11), VBA 7.1 — last verified 2026-09-19.

Related guides: VBA Set · VBA Nothing · VBA CreateObject · VBA Dim · VBA Collection