vba vba中dim和set有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3872339/
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 10:50:53  来源:igfitidea点击:

What is the difference between dim and set in vba

excelvbavb6

提问by Ram

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?

有什么不同?我什么时候应该使用什么?

采纳答案by Michael

There's no reason to use setunless 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除非引用对象引用,否则没有理由使用。仅在该上下文中使用它是一种很好的做法。对于所有其他简单数据类型,只需使用赋值运算符。但是,dim(维度)所有变量是一个好主意:

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.

简单数据类型的示例是integerlongbooleanstring。这些只是数据类型,没有自己的方法和属性。

Dim i as Integer
i = 5

Dim myWord as String
myWord = "Whatever I want"

An example of an objectwould be a Range, a Worksheet, or a Workbook. These have their own methods and properties.

an 的一个例子object是 a Range、 aWorksheet或 a Workbook。这些都有自己的方法和属性。

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 objectdeclared you can access its properties and methods.

如果您尝试使用没有 的最后一行Set,VB 将抛出错误。现在您已经object声明了一个可以访问它的属性和方法。

myString = myRange.Value

回答by RubberDuck



However, I don't think this is what you're really asking.

但是,我认为这不是您真正要问的。

Sometimes I use:

    Dim r as Range
    r = Range("A1")

有时我使用:

    Dim r as Range
    r = Range("A1")

This will never work. Without Setyou will receive runtime error #91 Object variable or With block variable not set. This is because you mustuse Setto assign a variables value to an object reference. Then the code above willwork.

这永远不会奏效。如果没有,Set您将收到运行时错误#91 Object variable 或 With block variable not set。这是因为您必须使用Set将变量值分配给对象引用。然后上面的代码起作用。

I think the code below illustrates what you're reallyasking about. Let's suppose we don't declare a type and let rbe a Varianttype 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.

那么,让我们分解一下这里发生的事情。

  1. ris declared as a Variant

    `Dim r` ' TypeName(r) returns "Empty", which is the value for an uninitialized variant
    
  2. ris set to the Rangecontaining cell "A1"

    Set r = Range("A1") ' TypeName(r) returns "Range"
    
  3. ris set to the valueof the default propertyof Range("A1").

    r = Range("A1") ' TypeName(r) returns "String"
    
  1. r被声明为 Variant

    `Dim r` ' TypeName(r) returns "Empty", which is the value for an uninitialized variant
    
  2. r设置为Range包含单元格“A1”

    Set r = Range("A1") ' TypeName(r) returns "Range"
    
  3. 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 Setexample:

至于你的Set例子:

Other times I use

Set r = Range("A1")

其他时候我用

Set r = Range("A1")

This wouldn't work without first declaring that ris a Rangeor Variantobject... using the Dimstatement - unless you don't have Option Explicitenabled, which you should. Always. Otherwise, you're using identifiers that you haven't declared and they are all implicitly declared as Variants.

如果没有首先声明它r是一个RangeVariant对象,这将不起作用......使用该Dim语句 - 除非你没有Option Explicit启用,你应该启用。总是。否则,您将使用尚未声明的标识符,并且它们都被隐式声明为 Variants

回答by Tobias Schittkowski

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 (e.g. int, string), then you would just write:

您必须对对象使用 set,如果 r 是一个简单类型(例如 int、string),那么您只需编写:

Dim r As Integer
r=5

回答by Justin Niessner

Dimsimply declares the value and the type.

Dim简单地声明值和类型。

Setassigns a value to the variable.

Set为变量赋值。

回答by user2479175

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 As Folder,则使用关键字“Set”为其分配值。

回答by IqbalHamid

Dimis 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 Setkeyword is used to assignan object variable to a new object.

另一方面,设置与变量声明无关。该Set关键字用于分配一个对象变量的新对象。

Hope that clarifies the difference for you.

希望这可以为您澄清差异。

回答by Soroush

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!

实际上所有的变量都是一样的!