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?
的区别是什么?什么时候用什么?
7 个解决方案
#1
56
There's no reason to use set
unless referring to an object reference. 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:
除非引用对象引用,否则没有理由使用set。最好只在这种情况下使用它。对于所有其他简单的数据类型,只需使用赋值运算符。不过,最好把所有变量都调暗:
Examples of simple data types would be 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
. 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. Now that you have an object
declared you can access its properties and methods.
如果您尝试使用没有设置的最后一行,VB将抛出一个错误。现在已经声明了对象,您可以访问它的属性和方法。
myString = myRange.Value
#2
47
-
昏暗的声明了变量。
Dim r As Range
-
Set
sets the variable to an object reference.将变量设置为对象引用。
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. This is because you must use Set
to assign a variables value to an object reference. Then the code above will work.
这永远不会工作。没有Set,您将接收到运行时错误#91对象变量或未设置块变量。这是因为您必须使用Set将变量值分配给对象引用。然后上面的代码就可以工作了。
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成为一个变体类型。
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 Variantr被声明为一个变体。
`Dim r` ' TypeName(r) returns "Empty", which is the value for an uninitialized variant
-
r
is set to theRange
containing cell "A1"r被设置为包含单元格A1的范围
Set r = Range("A1") ' TypeName(r) returns "Range"
-
r
is set to the value of the default property ofRange("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.
在这种情况下,范围的默认属性是. 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:
至于你的例子:
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. Always. Otherwise, you're using identifiers that you haven't declared and they are all implicitly declared as Variants.
如果不首先声明r是一个范围或变量对象,这是不可能的。使用Dim语句——除非您没有显式启用选项,否则应该这样做。总是这样。否则,您将使用尚未声明的标识符,它们都隐式地声明为变体。
#3
7
Dim: you are defining a variable (here: r is a variable of type Range)
Dim:定义一个变量(这里:r是类型范围的变量)
Set: you are setting the property (here: set the value of r to Range("A1") - this is not a type, but a value).
Set:您正在设置属性(这里:将r的值设置为Range(“A1”)——这不是类型,而是值)。
You have to use set with objects, if r were a simple type (e.g. int, string), then you would just write:
如果r是一种简单的类型(比如int, string),那么你只需写:
Dim r As Integer
r=5
#4
3
Dim
simply declares the value and the type.
Dim简单地声明了值和类型。
Set
assigns a value to the variable.
设置为变量赋值。
#5
1
If a variable is defined as an object e.g. Dim myfldr As Folder, it is assigned a value by using the keyword, "Set".
如果一个变量被定义为一个对象(例如Dim myfldr作为文件夹),那么它会通过使用关键字“Set”来分配一个值。
#6
0
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!
实际上所有的桨都是一样的!
#7
0
Dim
is short for Dimension and is used in VBA and VB6 to declare local variables.
Dim是维度的缩写,用于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与变量声明没有关系。Set关键字用于向新对象分配对象变量。
Hope that clarifies the difference for you.
希望你能明白其中的区别。
#1
56
There's no reason to use set
unless referring to an object reference. 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:
除非引用对象引用,否则没有理由使用set。最好只在这种情况下使用它。对于所有其他简单的数据类型,只需使用赋值运算符。不过,最好把所有变量都调暗:
Examples of simple data types would be 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
. 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. Now that you have an object
declared you can access its properties and methods.
如果您尝试使用没有设置的最后一行,VB将抛出一个错误。现在已经声明了对象,您可以访问它的属性和方法。
myString = myRange.Value
#2
47
-
昏暗的声明了变量。
Dim r As Range
-
Set
sets the variable to an object reference.将变量设置为对象引用。
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. This is because you must use Set
to assign a variables value to an object reference. Then the code above will work.
这永远不会工作。没有Set,您将接收到运行时错误#91对象变量或未设置块变量。这是因为您必须使用Set将变量值分配给对象引用。然后上面的代码就可以工作了。
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成为一个变体类型。
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 Variantr被声明为一个变体。
`Dim r` ' TypeName(r) returns "Empty", which is the value for an uninitialized variant
-
r
is set to theRange
containing cell "A1"r被设置为包含单元格A1的范围
Set r = Range("A1") ' TypeName(r) returns "Range"
-
r
is set to the value of the default property ofRange("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.
在这种情况下,范围的默认属性是. 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:
至于你的例子:
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. Always. Otherwise, you're using identifiers that you haven't declared and they are all implicitly declared as Variants.
如果不首先声明r是一个范围或变量对象,这是不可能的。使用Dim语句——除非您没有显式启用选项,否则应该这样做。总是这样。否则,您将使用尚未声明的标识符,它们都隐式地声明为变体。
#3
7
Dim: you are defining a variable (here: r is a variable of type Range)
Dim:定义一个变量(这里:r是类型范围的变量)
Set: you are setting the property (here: set the value of r to Range("A1") - this is not a type, but a value).
Set:您正在设置属性(这里:将r的值设置为Range(“A1”)——这不是类型,而是值)。
You have to use set with objects, if r were a simple type (e.g. int, string), then you would just write:
如果r是一种简单的类型(比如int, string),那么你只需写:
Dim r As Integer
r=5
#4
3
Dim
simply declares the value and the type.
Dim简单地声明了值和类型。
Set
assigns a value to the variable.
设置为变量赋值。
#5
1
If a variable is defined as an object e.g. Dim myfldr As Folder, it is assigned a value by using the keyword, "Set".
如果一个变量被定义为一个对象(例如Dim myfldr作为文件夹),那么它会通过使用关键字“Set”来分配一个值。
#6
0
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!
实际上所有的桨都是一样的!
#7
0
Dim
is short for Dimension and is used in VBA and VB6 to declare local variables.
Dim是维度的缩写,用于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与变量声明没有关系。Set关键字用于向新对象分配对象变量。
Hope that clarifies the difference for you.
希望你能明白其中的区别。