Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 859
gideon Avatar asked Mar 12 '11 09:03

gideon


People also ask

How do I fix Runtime error 91 in Excel VBA?

Error: "Runtime Error 91" is a Visual BASIC error which means "Object variable not set". This indicates that the object was never created using the "Set" command before being used. Remedy: Be sure to use the SET statement to create the new oject.

How do I fix object variable not set error 91 in Excel?

This error has the following causes and solutions: You attempted to use an object variable that isn't yet referencing a valid object. Dim MyObject As Object ' Create object variable. Set MyObject = Sheets(1) ' Create valid object reference.

How do you fix object variables with block not set?

To correct this errorMake sure you aren't referring to an object variable that has been set to Nothing . Search your code for the keyword Nothing , and revise your code so that the object isn't set to Nothing until after you have referenced it. Make sure that any array variables are dimensioned before you access them.

What does object variable or with block variable not set mean in VBA?

"Object variable or With block variable not set" is a Visual Basic error message. It could originate in Excel itself, in Palisade code, or in your own macros if you have an . XLSM file. Roughly, it means that the VBA code used some variable without first initializing it properly.


1 Answers

You need Set with objects:

 Set rng = Sheet8.Range("A12") 

Sheet8 is fine.

 Sheet1.[a1] 
like image 80
Fionnuala Avatar answered Nov 03 '22 22:11

Fionnuala