Excel VBA 中的范围是什么数据类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9814238/
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
What data type is a Range in Excel VBA?
提问by Joe
I'm trying to use some code like this, and it's failing:
我正在尝试使用一些这样的代码,但它失败了:
data = Range("A1")
MsgBox data.Offset(1,1)
This seems like it should print the value of cell B2, but instead it gives me an error (Run-time error 424: Object required.
).
这似乎应该打印单元格 B2 的值,但它给了我一个错误 ( Run-time error 424: Object required.
)。
So what data type does the expression Range("A1")
return, and how do I declare data
to be of the correct data type to store it?
那么表达式Range("A1")
返回什么数据类型,我如何声明data
它是正确的数据类型来存储它?
回答by mechanical_meat
Range
is an object.
That means you need use set
to assign an object reference:
Range
是一个对象。
这意味着您需要使用set
来分配对象引用:
Dim data As Range
Set data = Range("A1")
背景资料:
Set Keyword: In VBA, the Set keyword is necessary to distinguish between assignment of an object and assignment of the default property of the object.
Set关键字:在VBA中,Set关键字是区分对象赋值和对象默认属性赋值所必需的。
回答by paxdiablo
The data type for a range is, surprisingly enough, Range
:-)
范围的数据类型令人惊讶的是,Range
:-)
If you everget a message that an object is required, then you almost certainly need to use set
:
如果你曾经得到一个对象需要一个消息,那么你几乎肯定需要使用set
:
set data = Range("a1")
This is a classic 'gotcha' that most people encounter. The full code snippet would be:
这是大多数人都会遇到的经典“陷阱”。完整的代码片段将是:
Dim data As Range
Set data = Range("a1")