1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Quản trị mạng >

Figure 9-1. A small portion of the Excel object model (the tag <vX> means that the object is new in version X of Excel)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.72 MB, 490 trang )

9.4 Object Model Syntax

It is time that we formally discuss the basic syntax that is used when programming with an object


The general syntax for referring to an object's properties and methods is very simple. If objVar is

an object variable that refers to a particular object and AProperty is a property of this object,

then we can access this property (for reading or for changing) using the syntax:

objVar.AProperty(any required parameters)

For instance, the following code sets the font name property of the first row in the active


' Declare object variable

Dim rng As Range

' Set rng to refer to first row

Set rng = ActiveSheet.Rows(1)

' Set font name

rng.Font.Name = "Arial"

Note that the last line of code actually invokes two properties; the Font property of rng returns a

Font object, whose Name property is set to Arial.

If AMethod is a method for this object, then we can invoke that method with the syntax:


objVar.AMethod(any required parameters)

Note that this syntax is quite similar to the syntax used to call an ordinary VBA subroutine or

function, except that here we require qualification with the name of the variable that points to the

object whose property or method is being called.

For instance, continuing the previous code, we can apply the CheckSpelling method to the row

referred to by rng as follows:


We could include the name of a custom dictionary as a parameter to this method.

9.5 Object Variables

To access a property of an object, or to invoke a method, we can generally take two approaches:

direct or indirect. The indirect approach uses an object variable—that is, a variable that has an

object data type—whereas the direct approach does not.

For instance, to set the Bold property of the Font object for the first row in the active worksheet,

we can take a direct approach, as in:

ActiveSheet.Rows(1).Font.Bold = True

Alternatively, we can assign an object variable. Here are two possibilities:

Dim rng As Range

Set rng = ActiveSheet.Rows(1)

rng.Font.Bold = True

Dim fnt As Font

Set fnt = ActiveSheet.Rows(1).Font

fnt.Bold = True

Object variables are more important than they might seem at first. The most obvious reason for

their use is that they can improve code readability when we need to refer to the same object more

than once. For instance, instead of writing:

ActiveSheet.Rows(1).Font.Bold = True

ActiveSheet.Rows(1).Font.Italic = True

ActiveSheet.Rows(1).Font.Underline = False

ActiveSheet.Rows(1).Font.Size = 12

ActiveSheet.Rows(1).Font.Name = "Arial"

we can use a Font variable to improve readability as follows:

Dim fnt As Font

Set fnt = ActiveSheet.Rows(1).Font

fnt.Bold = True

fnt.Italic = True

fnt.Underline = False

fnt.Size = 12

fnt.Name = "Arial"


9.5.1 The With Statement

In fact, VBA provides a With statement to handle just the situation in the previous example,

which could be written as follows:

Dim fnt As Font

Set fnt = ActiveSheet.Rows(1).Font

With fnt

.Bold = True

.Italic = True

.Underline = False

.Size = 12

.Name = "Arial"

End With

The general syntax of the With statement is:

With object

' statements go here

End With

where the statements generally refer to the object, but do not require qualification using the

object's name, as in the previous example.

9.5.2 Object Variables Save Execution Time

The main reason that objec t variables are important is not to improve readability, but to save

execution time. In particular, to execute each of the five lines in the first version of the previous

code, VBA needs to resolve the references to the various Excel objects ActiveSheet, Rows(1), and

Font. That is, VBA needs to "climb down" the Excel object model. This takes time.

However, in the code that uses an object variable of type Font, VBA only needs to resolve these

references once. Therefore, the second version runs much more quickly. This difference can be

very noticeable when there are hundreds or thousands of references to resolve.

9.5.3 An Object Variable Is a Pointer

There are some very important differences between object variables and nonobject variables, such

as those of type Integer, Single, or String. As we have mentioned, a nonobject variable can be

thought of as a name for a location in the computer's memory that holds some data. For instance,

in the code:

Dim iVar As Integer

iVar = 123

the variable iVar is a 4-byte memory location that holds the integer value 123. This can be

pictured as in Figure 9-2. (Actually, the 4-byte memory location holds the value 123 in binary

format, but that is not relevant to our discussion.)

Figure 9-2. Integer variables in memory


Further, if we were to write:

Dim iVar2 As Integer

iVar2 = iVar

iVar2 = 567

we would not expect the last line of code to have any effect upon the value of the variable iVar,

which should still be 123. This is because iVar and iVar2 represent different areas of memory,

as pictured in Figure 9-2.

However, an object variable is not the name of a memory location that holds the object. Rather, an

object variable is the name of a memory location that holds the address of the memory location

that holds the object, as shown in Figure 9-3. Put another way, the object variable holds a

reference to or points to the object. For this reason, it is an example of a pointer variable, or

simply a pointer. In Figure 9-3, the object variable rng points to an object of type Range, namely,

the first column in the active sheet.

Figure 9-3. An object variable in memory

The code that goes with Figure 9-3 is:

Dim rng as Range

Set rng = ActiveSheet.Columns(1)

One of the consequences of the fact that object variables are pointers is that more than one object

variable can point to (or refer to) the same object, as in:





rng as Range

rng2 as Range

rng = ActiveSheet.Columns(1)

rng2 = rng

This code creates the situation pictured in Figure 9-4.

Figure 9-4. Two object variables referencing the same object

We emphasize that while rng and rng2 are different object variables, they hold the same value

and so, point to the same object. Thus, we can change the first column using either of these object


It is important when programming with objects to keep very careful track of all object variables

and what they are referencing. Furthermore, it is generally not a good idea to have more than one

object variable pointing to the same object (as in Figure 9-4) unless there is a compelling reason to

do so. It is very easy to change the object using one object variable (say rng) and then later use

the other variable (rng2), thinking it refers to the unchanged object.


Xem Thêm
Tải bản đầy đủ (.pdf) (490 trang)