simple vba code gives me run time error 91 object variable or with block not set

gideon picture gideon · Mar 12, 2011 · Viewed 104.7k times · Source

So I have a simple little macro/sub defined when a command button is clicked. The problem is it gives me:

Run Time Error '91' : Object Variable or With Block not Set

My code is:

Dim rng As Range
rng = Sheet8.Range("A12") '<< ERROR here
rng.Value2 = "1"

I just want to set Cell "A12" in Sheet8.

enter image description here

Thanks!

Answer

Fionnuala picture Fionnuala · Mar 12, 2011

You need Set with objects:

 Set rng = Sheet8.Range("A12")

Sheet8 is fine.

 Sheet1.[a1]