vbaexcelexcel-2007

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


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!


Solution

  • You need Set with objects:

     Set rng = Sheet8.Range("A12")
    

    Sheet8 is fine.

     Sheet1.[a1]