简单的 vba 代码给了我运行时错误 91 对象变量或块未设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5281759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:13:50  来源:igfitidea点击:

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

excelvbaexcel-vbaexcel-2007

提问by gideon

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
运行时错误“91”:对象变量或块未设置

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.

我只想在 Sheet8 中设置单元格“A12”。

enter image description here

在此处输入图片说明

Thanks!

谢谢!

回答by Fionnuala

You need Set with objects:

您需要 Set 对象:

 Set rng = Sheet8.Range("A12")

Sheet8 is fine.

Sheet8 没问题。

 Sheet1.[a1]