2 ways for “ClearContents” on VBA Excel, but 1 work fine. Why?

calendar_today Asked Sep 23, 2013
thumb_up 8 upvotes
history Updated April 16, 2026

Question posted 2013 · +4 upvotes

Good evening friends:

I have in mind 2 ways for clearing a content in a defined range of cells of a VBA project (in MS Excel):

  1. Worksheets("SheetName").Range("A1:B10").ClearContents
  2. Worksheets("SheetName").Range(Cells(1, 1), Cells(10, 2)).ClearContents

The problem is that the second way show me an error ‘1004‘ when I’m not watching the current Worksheet “SheetName” (in other words, when I haven’t “SheetName” as ActiveSheet).

The first way work flawlessly in any situation.

Why does this happen? How can I use the “Second way” without this bug?

Accepted answer +8 upvotes

That is because you are not fully qualifying your cells object. Try this

With Worksheets("SheetName")
    .Range(.Cells(1, 1), .Cells(10, 2)).ClearContents
End With

Notice the DOT before Cells?

Excel VBA objects referenced (4)

  • Range — Refer to Cells by Using a Range Object
  • Range — Delete Duplicate Entries in a Range
  • Worksheet — Refer to All the Cells on the Worksheet
  • Worksheet — List of worksheet functions available to Visual Basic

Top excel-vba Q&A (6)

+8 upvotes ranks this answer #88 out of 136 excel-vba solutions on this site .