[英]What is the difference between dim and set in vba
Pardon me as am a newbie in VBA.请原谅我是 VBA 的新手。
Sometimes I use有时我用
Dim r as Range
r = Range("A1")
Other times I use其他时候我用
Set r = Range("A1")
What is the difference?有什么不同? And when should I use what?
我什么时候应该使用什么?
There's no reason to use set
unless referring to an object reference.除非引用对象引用,否则没有理由使用
set
。 It's good practice to only use it in that context.仅在该上下文中使用它是一种很好的做法。 For all other simple data types, just use an assignment operator.
对于所有其他简单数据类型,只需使用赋值运算符。 It's a good idea to
dim
(dimension) ALL variables however:但是,将(维度)所有变量
dim
是一个好主意:
Examples of simple data types would be integer
, long
, boolean
, string
.简单数据类型的示例包括
integer
、 long
、 boolean
、 string
。 These are just data types and do not have their own methods and properties.这些只是数据类型,没有自己的方法和属性。
Dim i as Integer
i = 5
Dim myWord as String
myWord = "Whatever I want"
An example of an object
would be a Range
, a Worksheet
, or a Workbook
. object
一个示例是Range
、 Worksheet
或Workbook
。 These have their own methods and properties.这些都有自己的方法和属性。
Dim myRange as Range
Set myRange = Sheet1.Range("A1")
If you try to use the last line without Set
, VB will throw an error.如果您尝试使用没有
Set
的最后一行,VB 将抛出错误。 Now that you have an object
declared you can access its properties and methods.现在您已经声明了一个
object
您可以访问它的属性和方法。
myString = myRange.Value
Dim
declares the variable . Dim
声明变量。
Dim r As Range
Set
sets the variable to an object reference . Set
将变量设置为对象引用。
Set r = Range("A1")
However, I don't think this is what you're really asking.但是,我认为这不是您真正要问的。
Sometimes I use:
有时我使用:
Dim r as Range r = Range("A1")
This will never work.这永远不会奏效。 Without
Set
you will receive runtime error #91 Object variable or With block variable not set .如果没有
Set
您将收到运行时错误#91 Object variable 或 With block variable not set 。 This is because you must use Set
to assign a variables value to an object reference.这是因为您必须使用
Set
将变量值分配给对象引用。 Then the code above will work.然后上面的代码将起作用。
I think the code below illustrates what you're really asking about.我认为下面的代码说明了您真正要问的问题。 Let's suppose we don't declare a type and let
r
be a Variant
type instead.假设我们不声明类型,而是让
r
成为Variant
类型。
Public Sub test()
Dim r
debug.print TypeName(r)
Set r = Range("A1")
debug.print TypeName(r)
r = Range("A1")
debug.print TypeName(r)
End Sub
So, let's break down what happens here.所以,让我们分解一下这里发生的事情。
r
is declared as a Variant r
被声明为 Variant
`Dim r` ' TypeName(r) returns "Empty", which is the value for an uninitialized variant
r
is set to the Range
containing cell "A1" r
设置为包含单元格“A1”的Range
Set r = Range("A1") ' TypeName(r) returns "Range"
r
is set to the value of the default property of Range("A1")
. r
设置为Range("A1")
的默认属性的值。
r = Range("A1") ' TypeName(r) returns "String"
In this case, the default property of a Range is .Value
, so the following two lines of code are equivalent.在这种情况下, Range 的默认属性是
.Value
,因此以下两行代码是等效的。
r = Range("A1")
r = Range("A1").Value
For more about default object properties, please see Chip Pearson's "Default Member of a Class" .有关默认对象属性的更多信息,请参阅Chip Pearson 的“类的默认成员” 。
As for your Set
example:至于您的
Set
示例:
Other times I use
其他时候我用
Set r = Range("A1")
This wouldn't work without first declaring that r
is a Range
or Variant
object... using the Dim
statement - unless you don't have Option Explicit
enabled, which you should.如果不首先使用
Dim
语句声明r
是Range
或Variant
对象,这将不起作用 - 除非您没有启用Option Explicit
,您应该这样做。 Always.总是。 Otherwise, you're using identifiers that you haven't declared and they are all implicitly declared as Variants .
否则,您将使用尚未声明的标识符,并且它们都被隐式声明为Variants 。
Dim: you are defining a variable (here: r is a variable of type Range) Dim:您正在定义一个变量(此处:r 是 Range 类型的变量)
Set: you are setting the property (here: set the value of r to Range("A1") - this is not a type, but a value).设置:您正在设置属性(此处:将 r 的值设置为 Range("A1") - 这不是类型,而是值)。
You have to use set with objects, if r were a simple type (eg int, string), then you would just write:您必须对对象使用 set,如果 r 是一个简单类型(例如 int、string),那么您只需编写:
Dim r As Integer
r=5
Dim
simply declares the value and the type. Dim
只是声明了值和类型。
Set
assigns a value to the variable. Set
为变量赋值。
如果将变量定义为对象,例如 Dim myfldr As Folder,则使用关键字“Set”为其分配值。
Dim
is short for Dimension and is used in VBA and VB6 to declare local variables. Dim
是 Dimension 的缩写,在 VBA 和 VB6 中用于声明局部变量。
Set on the other hand, has nothing to do with variable declarations.另一方面,设置与变量声明无关。 The
Set
keyword is used to assign an object variable to a new object. Set
关键字用于将对象变量分配给新对象。
Hope that clarifies the difference for you.希望这可以为您澄清差异。
According to VBA help on SET statement it sets a reference to an object.so if you change a property the actual object will also changes.根据 VBA 对 SET 语句的帮助,它设置了对对象的引用。因此,如果更改属性,实际对象也将更改。
Dim newObj as Object
Set var1=Object1(same type as Object)
Set var2=Object1(same type as Object)
Set var3=Object1(same type as Object)
Set var4=Object1(same type as Object)
Var1.property1=NewPropertyValue
the other Vars properties also changes,so:其他 Vars 属性也会发生变化,因此:
Var1.property1=Var2.property1=Var3.property1=Var4.property1=Object1.Property1=NewpropertyValue`
actualy all vars are the same!实际上所有的变量都是一样的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.