关键字“新建"在VBA中起什么作用? [英] What does the keyword 'New' do in VBA?
问题描述
在VBA程序中,通常会在实例化对现有对象实例的对象引用时不断遇到关键字 New .但是在某些实例中,我们使用关键字"New",而在其他实例中,例如,我们不使用关键字:
In VBA procedures we are constantly meeting the keyword New usually 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:
Dim T As Excel.Workbook
Set T = Application.Workbooks(Bk)
在上方的示例1中,新建" 关键字未使用
In the upper example No.1 the "New" keyword has not been used
Dim fso As FileSystemObject
Set fso = New FileSystemObject
在上面的示例2中,正在使用 New 关键字
In the upper example No.2 the New keyword is being used
为什么呢?请记住,我对VBA一无所知,但我会尽我所能了解!
Why that? Keep in mind i'm fresh of the boat in VBA but i will do my best to understand!
此外,在声明对象引用时使用/不使用时,我也会感到困惑:
In addition to that i also get confused when is used/not-used in declaring an object reference for example:
Dim WS As Worksheet
在上方的示例1中,新" 关键字未使用
In the upper example No.1 the "New" keyword has not been used
Dim myClassModule As New cl_ChartEvents
在上面的示例2中,正在使用 New 关键字
In the upper example No.2 the New keyword is being used
Microsoft帮助什么都没有告诉我...
The Microsoft Help just tells me nothing...
用于隐式创建对象的关键字.如果在声明对象变量时使用新建",则会在首次引用该对象时创建该对象的新实例,因此您不必使用Set语句来分配该对象引用.
Gratz2u
亲爱的人们,这是最后一次尘埃落定,需要深入了解
Dear people just a last dust-off for deep understanding
Dim WS as Worksheet
Set WS = Worksheets("Sheet1")
在这里,我们正在创建一个已经存在的对象,以打开MS Excel(当然,在默认模式"下为例),这就是Sheet1. 因为它存在并且New
关键字是不可能的,我们如何立即在一行中实例化该对象?
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 New
keyword is out of the question how could we instantiate this object in one line right away?
4 @exantas
4 @exantas
对不起,没有足够的代表来发表照片:-(
Sorry says not enough rep to post pic :-(
推荐答案
使变量变暗时,您将指定它将保留的数据类型.在创建时,其值始终为Nothing
,直到您对其进行初始化. Set T = Application.Workbook(Bk)
将T变量初始化为Application.Workbook的特定实例,在本例中为"Bk".
When you Dim a variable you are specifying what data type it will hold. At the time of creation, its value is always Nothing
until you initialize it. Set T = Application.Workbook(Bk)
initializes the T variable to the specific instance of Application.Workbook, in this case 'Bk'.
当您使用Dim fso as FileSystemObject
时,您基本上是说fso
会在某个时候保留一个FileSystemObject
;但是,其初始值为Nothing
,直到使用Set fso = New FileSystemObject
对其进行初始化为止. New
关键字表示您正在创建一个新对象,而不是像使用Set T = Application.Workbook(Bk)
When you Dim fso as FileSystemObject
, you are basically saying that fso
will hold, at some point, a FileSystemObject
; however its initial value is Nothing
until you initialize it using Set fso = New FileSystemObject
. The New
keyword 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)
还请注意,Set fso = FileSystemObject
无效,因为它不知道要分配给FileSystemObject
的哪个实例.这就是为什么使用New
关键字创建FileSystemObject的新实例的原因.
Also note that Set fso = FileSystemObject
would be invalid because it doesn't know what instance of FileSystemObject
you wish to assign to it. This is why you use the New
keyword to create a new instance of FileSystemObject.
如前所述,Dim WS As Worksheet
仅告诉编译器您希望变量WS容纳Worksheet对象.由于未指定其他任何内容,因此在Dim点,WS设置为Nothing
As stated before, Dim WS As Worksheet
merely 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 myClassModule As New cl_ChartEvents
等同于执行以下操作:
Dim myClassModule As New cl_ChartEvents
is equivalent to doing:
Dim myClassModule as cl_ChartEvents
Set myClassModule = New cl_ChartEvents
...,但一行代码而不是两行代码除外.这与Dim WS As Worksheet
的不同之处在于立即将变量初始化,即myClassModule
设置为cl_ChartEvents
的新实例,而不是Nothing
.
... except on one line of code instead of two. This differs from Dim WS As Worksheet
in that the variable is initialized straight away, i.e. myClassModule
is set to a new instance of cl_ChartEvents
instead of Nothing
.
希望这会有所帮助!
这篇关于关键字“新建"在VBA中起什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!