vba中的dim和set之间有什么区别?

编程入门 行业动态 更新时间:2024-10-26 15:28:19
本文介绍了vba中的dim和set之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 赦免我是VBA的新手。

有时我使用

Dim r as Range r =范围(A1)

其他时间我使用

设置r =范围(A1)

<有什么区别?什么时候应该使用什么?

解决方案

没有理由使用 set 除非引用对象引用。在这种情况下只能使用它是一个很好的做法。对于所有其他简单的数据类型,只需使用赋值运算符。这是一个好主意, dim (维)所有变量:

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

Dim i as Integer i = 5 Dim myWord as String myWord =无论我想要什么

对象的示例将是范围,工作表或工作簿。这些有自己的方法和属性。

Dim myRange as Range 设置myRange = Sheet1.Range(A1)

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

myString = myRange.Value

Pardon me as am a newbie in 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. 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:

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.

myString = myRange.Value

更多推荐

vba中的dim和set之间有什么区别?

本文发布于:2023-10-29 02:52:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538516.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   vba   dim   set

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!