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

Table 8-2. Some Date- and Time-Related Functions

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 )

DateAdd, DateDiff, DatePart

DateSerial, DateValue

TimeSerial, TimeValue

Date, Time


Perform date calculations.

Return a date.

Return a time.

Set the date or time.

Time a process.

8.6.3 The Format Function

The Format function is used to format strings, numbers, and dates. Table 8-3 gives a few






Table 8-3. Format Function Examples


Return value

Format(Date, "Long Date")

Thursday, April 30, 1998

Format(Time, "Long Time")

5:03:47 PM

Format(Date, "mm/dd/yy hh:mm:ss AMPM")

04/30/98 12:00:00 AM

Format(1234.5, "$##,##0.00")


Format("HELLO", "<")


87 ®


Part III: Excel Applications and the Excel Object


Chapter 9

Chapter 10

Chapter 11

Chapter 12

Chapter 13

Chapter 14

Chapter 15

Chapter 16

Chapter 17

Chapter 18

Chapter 19

Chapter 20

Chapter 21

Chapter 22


Chapter 9. Object Models

In this chapter, we present a general overview of object models and the syntax used to manipulate

them in code.

As we have discussed, VBA is the programming language that underlies several important

Windows applications, including Microsoft Excel, Word, Access, PowerPoint, Visual Basic, and,

in Office 2000, Outlook. Any application that uses VBA in this way is called a host application.

We also discussed the fact that each host application enhances VBA by providing an object model

(perhaps more than one) to deal with the objects that are particular to that application.

Microsoft provides over a dozen different object models for its Office application suite and related

products. These include object models for Excel, Word, Access, DAO (Data Access Objects),

Outlook, PowerPoint, Binder, Graph, Forms, VBA, VB, ASP (Active Server Pages), and more. Of

course, our interest in this book is with the Excel object model, and we will devote most of the rest

of the book to describing the major portions of this model. (We will also discuss a portion of the

Office object model in the chapter on customizing Excel menus and toolbars.)

9.1 Objects, Properties, and Methods

In the parlance of VBA programming, an object is something that is identified by its properties

and its methods. For example, workbooks, worksheets, charts, and fonts are all examples of

objects in the Excel object model. Actually, the Excel object model contains 192 different objects,

including several hidden and obsolete ones.

9.1.1 Properties

The term property is used in the present context in pretty much the same way that it is used in

everyday English; it is a trait or attribute or characteristic of an object. For instance, a Worksheet

object has 55 properties, among which are Cells, Name, ProtectionMode, and UsedRange. A

property's value can be any valid data type, such as Integer, Single, String, or even another object


When the value of a property has type Integer, for instance, we will refer to the property as an

integer property. Integer properties are quite common, and so Microsoft has defined a large

number of built-in enums (152, to be exact, with 1266 individual constants) to give symbolic

names to these property values. For instance, the Calculation property of the Application object

can take on any of the values in the enum defined by:

Enum XlCalculation

xlCalculationManual = -4135

xlCalculationAutomatic = -4105

xlCalculationSemiautomatic = 2

End Enum

If a property's value is an object, it is referred to as an object property. For instance, a Workbook

object has an ActiveChart property that returns a Chart object. Of course, the Chart object has its

own set of properties and methods.


Because a Chart object can be obtained from a Workbook object, we refer to Chart as a child

object of Workbook and Workbook as a parent of Chart. We will have more to say about this

parent-child relationship a bit later.

9.1.2 Methods

A method of an object is an action that can be performed on (or on behalf of ) the object. For

instance, a Worksheet object has a Protect method that causes the worksheet to be protected.

In programming terms, the properties and methods of an object are just built-in functions or

subroutines. It is important to emphasize that the distinction between property and method is one

of intent and is often made somewhat arbitrarily. (In fact, the Item member is sometimes classified

as a property and sometimes as a method, depending upon the object in question; it appears that

even Microsoft has trouble making up its collective mind from time to time.)

The properties and methods of an object are collectively referred to as the object's members. This

should not be confused with an object's children.

9.2 Collection Objects

In programming with the Excel object model (or indeed any object model), it is common to have a

great many objects "alive" at the same time. For instance, each cell within the current selection is

an object (a Range object), as is each row and column in each open worksheet. Hence, at any

given time, there are thousands of objects in existence. To manage these objects, the designers of

an object model generally include a special type of object called a collection object.

As the name implies, collection objects represent collections of objects—generally objects of a

single type. For instance, the Excel object model has a collection object called Rows that

represents the set of all rows in the worksheet in question (as Range objects). It is customary to

say that the Rows collection object contains the rows in the sheet, so we will use this terminology

as well. There is one Rows collection for each open worksheet.

Collection objects are generally just called collections, but it is very important to remember that a

collection is just a special type of object. As we will see, the properties and methods of a

Collection object are specifically designed to manage the collection.

We can generally spot a collection object by the fact that its name is the plural of the name of the

objects contained within the collection. For instance, the Worksheets collection contains

Worksheet objects. However, in some cases, this naming convention is not followed. For instance,

the Rows collection contains Range objects. In the Excel object model, there are no Cell, Row, or

Column objects. These are all represented by Range objects. We will devote an entire chapter

(Chapter 19) to the important Range object.

Collections are extremely common in the Office object models. In fact, almost one-half of all of

the objects in the Excel object model are collections! Table 9-1 shows some of the more

commonly used collections in the Excel object model.




Table 9-1. Some Excel Collection Objects








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