VBA 中的关键字“New”有什么作用?

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

What does the keyword 'New' do in VBA?

excelvbaexcel-vba

提问by Codo

In VBA procedures we are constantly meeting the keyword Newusually on instantiation of an object reference to an existing object instance. But in some instantiations we use the keyword New while in others we don't for example:

在 VBA 过程中,我们经常遇到关键字New,通常是在实例化对现有对象实例的对象引用时。但是在某些实例中,我们使用关键字 New 而在其他实例中我们不使用,例如:

Dim T As Excel.Workbook

Dim T As Excel.Workbook

Set T = Application.Workbooks(Bk)

Set T = Application.Workbooks(Bk)

In the upper example No.1 the "New"keyword has notbeen used

在上面的例子 No.1 中,没有使用“New”关键字

Dim fso As FileSystemObject

Dim fso As FileSystemObject

Set fso = New FileSystemObject

Set fso = New FileSystemObject

In the upper example No.2 the Newkeyword isbeing used

在上面的示例 2,使用了New关键字

Why that? Keep in mind i'm fresh of the boat in VBA but i will do my best to understand!

为什么?请记住,我是 VBA 的新手,但我会尽力理解!

In addition to that i also get confused when is used/not-used in declaring an object reference for example:

除此之外,在声明对象引用时使用/不使用时我也会感到困惑,例如:

Dim WS As Worksheet

Dim WS As Worksheet

In the upper example No.1 the "New"keyword has notbeen used

在上面的例子 No.1 中,没有使用“New”关键字

Dim myClassModule As New cl_ChartEvents

Dim myClassModule As New cl_ChartEvents

In the upper example No.2 the Newkeyword isbeing used

在上面的示例 2,使用了New关键字

The Microsoft Help just tells me nothing...

Microsoft 帮助只是告诉我什么...

Keyword that enables implicit creation of an object. If you use New when declaring the object variable, a new instance of the object is created on first reference to it, so you don't have to use the Set statement to assign the object reference.

允许隐式创建对象的关键字。如果在声明对象变量时使用 New,则会在第一次引用时创建对象的新实例,因此您不必使用 Set 语句来分配对象引用。

Gratz2u

格拉茨2u

Dear people just a last dust-off for deep understanding

亲爱的人们只是为了深刻理解的最后一抹灰尘

Dim WS as Worksheet

Dim WS as Worksheet

Set WS = Worksheets("Sheet1")

Set WS = Worksheets("Sheet1")

Right here we are creating an object that already existed in order to open MS Excel (lets say for examples sake in "default mode") of course which is Sheet1. Since it exists and the Newkeyword is out of the question how could we instantiate this object in one line right away?

在这里,我们正在创建一个已经存在的对象,以便打开 MS Excel(例如,在“默认模式”下),当然是 Sheet1。既然它存在并且New关键字是不可能的,我们如何立即在一行中实例化这个对象?

4 @exantas

4 @exantas

Sorry says not enough rep to post pic :-(

抱歉说没有足够的代表发布图片:-(

回答by nagyben

When you Dim a variable you are specifying what data type it will hold. At the time of creation, its value is always Nothinguntil you initialize it. Set T = Application.Workbook(Bk)initializes the T variable to the specific instance of Application.Workbook, in this case 'Bk'.

当您对变量进行 Dim 时,您是在指定它将保存的数据类型。在创建时,它的值始终存在,Nothing直到您对其进行初始化。Set T = Application.Workbook(Bk)将 T 变量初始化为 Application.Workbook 的特定实例,在本例中为“Bk”。

When you Dim fso as FileSystemObject, you are basically saying that fsowill hold, at some point, a FileSystemObject; however its initial value is Nothinguntil you initialize it using Set fso = New FileSystemObject. The Newkeyword implies you're creating a new object instead of initializing the variable with an existing object as you do with Set T = Application.Workbook(Bk)

当您Dim fso as FileSystemObject,您基本上是说fso在某些时候将保持FileSystemObject; 但是它的初始值是Nothing直到您使用Set fso = New FileSystemObject. 该New关键字暗示您正在创建一个新对象,而不是像使用现有对象那样使用现有对象初始化变量Set T = Application.Workbook(Bk)

Also note that Set fso = FileSystemObjectwould be invalid because it doesn't know what instance of FileSystemObjectyou wish to assign to it. This is why you use the Newkeyword to create a new instance of FileSystemObject.

另请注意,这Set fso = FileSystemObject将是无效的,因为它不知道FileSystemObject您希望分配给它的实例。这就是您使用New关键字创建 FileSystemObject 的新实例的原因。

As stated before, Dim WS As Worksheetmerely tells the compiler that you want variable WS to hold a Worksheet object. Since nothing else is specified, at the point of Dim, WS is set to Nothing

如前所述,Dim WS As Worksheet只是告诉编译器您希望变量 WS 保存一个 Worksheet 对象。由于没有指定其他内容,因此在 Dim 处,WS 设置为Nothing

Dim myClassModule As New cl_ChartEventsis equivalent to doing:

Dim myClassModule As New cl_ChartEvents相当于做:

Dim myClassModule as cl_ChartEvents
Set myClassModule = New cl_ChartEvents

... except on one line of code instead of two. This differs from Dim WS As Worksheetin that the variable is initialized straight away, i.e. myClassModuleis set to a new instance of cl_ChartEventsinstead of Nothing.

...除了一行代码而不是两行。这与Dim WS As Worksheet变量立即初始化不同,即myClassModule设置为 的新实例cl_ChartEvents而不是Nothing

Hope this helps!

希望这可以帮助!

回答by Thomas Weller

You said: "[We are] meeting the keyword New usually on instantiation of an object reference to an existing object instance". Exactly the opposite is true: New is used when something does notexist yet and you want to create it, well "new".

您说:“[我们] 通常在实例化对现有对象实例的对象引用时遇到关键字 New”。恰恰相反:当某物尚不存在而您想创建它时使用 New ,即“新”。

You can omit the Newkeyword if something already exists, e.g. the Applicationobject, ActiveWorkbookobject and others in VBA, that is everything which was already opened by you when starting Excel.

New如果某些东西已经存在,您可以省略关键字,例如VBA 中的Application对象、ActiveWorkbook对象和其他对象,即您在启动 Excel 时已经打开的所有内容。

Dim ... As New ...is a shortcut for

Dim ... As New ...是捷径

Dim ... As ...
Set ... = New ...

For your last question to create a new worksheet, it's done by

对于创建新工作表的最后一个问题,它是由

Dim WS As Worksheet
Set WS = Sheets.Add
WS.Name = "My new worksheet"

You cannot use Dim WS as New Worksheet, because Microsoft prevents you from doing so. Even if you specify Newin that instruction, the object is still Nothing.

您不能使用Dim WS as New Worksheet,因为 Microsoft 阻止您这样做。即使您New在该指令中指定,对象仍然是Nothing