Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Set: Assign Objects to Variables

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To use Set in Excel VBA, you assign an object reference to a variable with the syntax Set variableName = objectExpression. For example, Set ws = ThisWorkbook.Worksheets("Sheet1") points the variable ws at that worksheet so you can call its properties and methods. Unlike a plain = assignment for values, Set is required whenever the variable is an object type (Worksheet, Range, Workbook, Chart, Collection, and so on). Without Set, VBA raises runtime error 91 (Object variable or With block variable not set) or a type mismatch. This guide walks through the correct syntax, common pitfalls, and practical patterns.

What You'll Need

  • Microsoft Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
  • Access to the Visual Basic Editor (Alt+F11).
  • A basic understanding of variables and data types in VBA.
  • A workbook to test in — macros can be disabled by policy; check with your administrator if the Developer tab is missing.

Step-by-Step

  1. Open the Visual Basic Editor. Press Alt+F11 (or use Developer > Visual Basic). In the Project Explorer, right-click your workbook and choose Insert > Module to add a standard module.
  2. Declare an object variable. At the top of the module, type Dim ws As Worksheet or Dim rng As Range. Using explicit types (early binding) gives you IntelliSense and catches errors at compile time. If you use Dim obj As Object, you get late binding and no IntelliSense.
  3. Assign the object with Set. Write Set ws = ThisWorkbook.Worksheets("Sheet1"). The Set keyword tells VBA you are assigning a reference, not a value. For a range: Set rng = ws.Range("A1:C10").
  4. Use the object variable. Now you can read or write properties: ws.Cells(1,1).Value = "Hello" or rng.Font.Bold = True. Because ws points to the worksheet, every property call acts on that sheet.
  5. Release the reference (optional but good practice). At the end of the procedure, set the variable to Nothing: Set ws = Nothing. This frees the reference and helps avoid memory leaks in long-running macros, though VBA usually cleans up when the procedure ends.
  6. Test with a simple macro. Press F5 to run. If you see error 91, check that every object variable is assigned with Set before you use it. Use Debug.Print ws.Name to verify the reference in the Immediate Window (Ctrl+G).
  7. Handle missing objects gracefully. If the sheet name might not exist, wrap the assignment in error handling: On Error Resume Next, then Set ws = ThisWorkbook.Worksheets("Sheet1"), then If ws Is Nothing Then MsgBox "Sheet not found". Reset with On Error GoTo 0.

Common Mistakes

  • Omitting Set. Writing ws = ThisWorkbook.Worksheets("Sheet1") without Set causes error 91 or 424. Always use Set for object variables.
  • Using Set for value types. Set x = 5 is invalid because 5 is not an object. Use plain = for numbers, strings, dates, and Booleans.
  • Assigning to an unqualified sheet. Set ws = Worksheets("Sheet1") refers to the active workbook, which may not be the one you expect. Qualify with ThisWorkbook or Workbooks("Name.xlsx").
  • Forgetting to release objects in loops. Repeatedly setting a Range variable inside a loop without releasing can slow things down; set to Nothing after each iteration if performance matters.
  • Assuming Set copies data. Set rng2 = rng1 makes both variables point to the same range; changing one affects the other. To copy values, use rng2.Value = rng1.Value.

Tips

  • Use meaningful variable names like wsData or rngInput to keep code readable.
  • Combine declaration and assignment on separate lines for clarity; VBA does not allow Dim ws As Worksheet = ....
  • Use the With block to avoid repeated Set statements: With ws then .Cells(1,1).Value = "x" and End With.
  • Enable Option Explicit at the top of every module to force variable declaration and catch typos.
  • When working with multiple workbooks, set a Workbook variable first: Set wb = Workbooks.Open("path"), then Set ws = wb.Worksheets(1).

Troubleshooting

  • Error 91 "Object variable or With block variable not set": You used an object variable before assigning it with Set. Check every path in your code, including error branches.
  • Error 424 "Object required": You tried to use Set on something that is not an object, or you passed a non-object to a method expecting one. Verify the variable type.
  • Error 438 "Object doesn't support this property or method": The object exists but does not have the property you called. For example, a Range has no Worksheets property. Check the object model.
  • Set appears to do nothing: You may have set a local variable inside a subroutine and expected it to persist. Object variables are scoped to the procedure unless declared at module level with Dim outside any Sub.
  • Macro runs on the wrong sheet: Qualify your Worksheet references with a Workbook object, and avoid relying on ActiveSheet.

Frequently Asked Questions

What is the difference between Set and Let in VBA?

Let assigns a value to a variable (and is optional; x = 5 is the same as Let x = 5). Set assigns an object reference. You must use Set for objects; using Let or plain = for an object causes an error.

Can I use Set with a Range variable?

Yes. Dim rng As Range: Set rng = Worksheets("Sheet1").Range("A1") is standard. After that, rng.Value = 10 writes to A1.

Do I need to set objects to Nothing?

Not strictly required for most macros because VBA releases references when the procedure ends. However, setting to Nothing is good practice for long-running or memory-intensive code, and it makes your intent clear.

Why does Set fail with "Object required"?

That error means the expression on the right of Set is not an object. For example, Set x = Range("A1").Value fails because .Value returns a number or string, not an object. Remove .Value to get the Range object.

Can I Set a variable to a new object I create?

Yes, for objects that support it. For example, Set coll = New Collection creates a new Collection. For Excel objects like Worksheet, you cannot create them with New; you must use the appropriate collection's Add method and then Set the variable to the result.

Does Set work with arrays?

No. Arrays are not objects in VBA; assign them with plain = (or use Set only if the array is wrapped in an object like a Collection). For a dynamic array, use ReDim.