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

VBA Set in Excel — Why Object Variables Need Set (and Errors 91 and 424)

|

VBA Set in Excel — Why Object Variables Need Set (and Errors 91 and 424)

TL;DR — In VBA you assign an object with Set, not a plain =. An object variable is a reference — a label that points at a live object — so Set ws = Worksheets("Data") points the label at the sheet. Forget the Set and VBA raises error 91, Object variable or With block variable not set. Use Set on something that is not an object and you get error 424, Object required. Values (Integer, String, Double) are copied with =; objects are pointed at with Set.

Sub ObjectsNeedSet()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Data")   ' Set = point the label at the sheet
    ws.Range("A1").Value = 100                  ' now ws refers to something - this works

    Dim total As Double
    total = ws.Range("A1").Value                ' values use =, no Set
End Sub

Set is the keystone of the object lifecycle: you make an object with New, you point a variable at it with Set, and you release it with Nothing. Get Set wrong and you hit the two errors every VBA developer meets first — 91 and 424 — so it pays to understand why objects need it.

What you'll learn

  • The mental model — an object variable is a reference (a label), not a box that holds a copy
  • Which things need Set (Range, Worksheet, Workbook, Dictionary) and which do not (Integer, String)
  • The number-one bug — error 91, Object variable or With block variable not set, from a missing Set
  • Error 424, Object required, and how it differs from 91
  • Why Set b = a makes two names for the same live object, not a copy

The mental model: a reference, not a box

A value variable is a box. Dim n As Long: n = 5 puts the number 5 inside n. Copy it with m = n and m gets its own 5; change m and n is untouched. Simple.

An object variable is not a box — it is a label. Dim ws As Worksheet creates an empty label that currently points at no sheet at all. Set ws = Worksheets("Data") does not copy the worksheet into ws; it points the label at the existing sheet. That is the whole reason Set exists: assigning an object is an act of pointing, and VBA makes you say so out loud.

Dim ws As Worksheet
ws = Worksheets("Data")        ' WRONG - no Set - run-time error 91
Set ws = Worksheets("Data")    ' RIGHT - the label now points at the sheet

Hold on to one sentence and everything else follows: values you copy with =; objects you point at with Set. The two runtime errors below are both just VBA telling you it could not point a label properly.

The number-one bug: error 91

91 is the error that sends people searching, under its full name: Object variable or With block variable not set. It means you used an object variable that is currently pointing at nothing. Two situations cause it, and both come back to a missing Set.

First, you declared the object but never Set it, so it is still Nothing, and then you used it:

Dim ws As Worksheet
ws.Range("A1").Value = 1       ' error 91 - ws was never Set, it points at nothing

Second — the sneaky one — you did mean to assign the sheet, but you forgot the Set:

Dim ws As Worksheet
ws = ThisWorkbook.Worksheets("Data")   ' error 91 - a bare = cannot point an object label

The fix for both is the same word: Set ws = ThisWorkbook.Worksheets("Data"). Whenever you see Object variable or With block variable not set, the first question is always: did I Set this object, and did the thing I Set it to actually exist? (A Find or a lookup that returns Nothing is the other classic source — the variable is set, to Nothing, and you used it anyway.)

Error 424: Object required

424, Object required, is the mirror image of 91. Where 91 is "you used an object slot that was empty", 424 is "you used Set, or object syntax, on a thing that is not an object at all":

Dim n As Long
Set n = 5                      ' error 424 - 5 is a value, Set wants an object
Set n = Range("A1").Value      ' error 424 - .Value is a number, not an object

Set on the left demands an object on the right. Give it a number, a string, or a property that returns a value, and VBA says Object required. You also see 424 when you call object syntax on a plain variable — x.Cells(1, 1) where x is a String, or a mistyped variable name that VBA silently treats as an empty Variant. The rule of thumb: 91 means "Set it first"; 424 means "that is not an object, drop the Set (or fix the right-hand side)".

Set makes an alias, not a copy

Because Set points rather than copies, two object variables can end up pointing at the same live object — and writing through one changes what the other sees:

Dim a As Range, b As Range
Set a = Range("A1:B2")
Set b = a                      ' b points at the SAME range as a - not a copy
b.ClearContents                ' this also clears a, because a and b are one range

Compare that with values, where a copy really is a copy:

Dim x As Long, y As Long
x = 5: y = x                   ' y gets its own 5
y = 9                          ' x is still 5 - independent boxes

This aliasing is not a bug; it is the point of references, and it is why passing objects around is cheap (you pass the label, never the whole sheet). But it does mean "I copied the range into another variable and edited it" quietly edits the original. To duplicate the data, copy the cells (a.Copy), not the reference.

Set versus Let

VBA actually has a matching keyword for values: Let. Let n = 5 is the same as n = 5 — the Let is optional and nobody writes it anymore, so value assignment reads as a bare =. There is no such shortcut for objects: Set is mandatory and can never be dropped. That asymmetry is exactly why the missing-Set mistake is so easy to make — every other assignment in your macro is a bare =, and objects are the one place it is not enough.

How ExcelMaster helps

The whole Set story is one decision made on every assignment — is this a value I copy, or an object I point at? — and getting it wrong fails loudly with 91 or 424, or quietly with an alias you did not expect.

ExcelMaster lets you describe the task — "grab the Data sheet, find the last row, write the total" — and it writes the object references with Set where they belong, declares each variable with the right type, and guards a lookup that might return Nothing before it touches the result, so error 91 never reaches you. You keep the workbook and the code.

Frequently asked questions

What does Set do in VBA?

Set assigns a reference to an object variable — it points the variable at a live object such as a Range, Worksheet, Workbook or Dictionary. Unlike a plain =, which copies a value, Set does not copy the object; it makes the variable refer to the existing one. Objects need Set; values (Integer, String, Double) do not.

Why do I get the Object variable or With block variable not set error?

That is run-time error 91, and it means you used an object variable that points at nothing. Usually you declared it with Dim but never assigned it with Set, or you wrote a bare = instead of Set (a bare = cannot point an object variable), or the thing you assigned — such as a Find result — returned Nothing. Add the Set, and check that the object you assign actually exists.

What is the difference between error 91 and error 424?

Error 91, Object variable or With block variable not set, means you used an object variable that is currently Nothing — the fix is to Set it first. Error 424, Object required, means you used Set (or object syntax) on something that is not an object — such as Set n = 5 — so the fix is to remove the Set or put a real object on the right-hand side.

Do I need Set for a String or an Integer?

No. String, Integer, Long, Double, Boolean, Date and other value types are assigned with a plain =, because they are copied, not referenced. Set is only for object types. Using Set on a value raises error 424, Object required.

Does Set copy the object?

No — Set points the variable at the existing object. Set b = a gives you two variables that refer to the same live object, so a change through b is visible through a. To duplicate the underlying data (for a range, say), copy the cells with a.Copy, not the reference.

Tested in

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

Related guides: VBA New · VBA Nothing · VBA Dim · VBA CreateObject · VBA With