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

Figure 19-2. The range as a union

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 )

19.2.4 Offset Property

The Offset property is used to return a range that is offset from a given range by a certain number

of rows and/or columns. The syntax is:

RangeObject.Offset(RowOffset, ColumnOffset)

where RowOffset is the number of rows and ColumnOffset is the number of columns by

which the range is to be offset. Note that both of these parameters are optional with default value 0,

and both can be either positive, negative, or 0.

For instance, the following code searches the first 100 cells to the immediate right of cell D2 for

an empty cell (if you tire of the message boxes, simply press Ctrl-Break to halt macro execution):

rng As Range

i As Integer

rng = Range("D2")

i = 1 To 100

If rng.Offset(0, i).Value = "" Then

MsgBox "Found empty cell at offset " & i & " from cell D2"

End If









19.3 Additional Members of the Range Object

Let us now take a quick look at some additional members of the Range object. (Please refer to

Table 19-1 for an indication of which members are discussed in this section.)


19.3.1 Activate Method

The Activate method will activate (or select) the range to which it is applied. The Activate method

applies to a variety of other objects besides the Range object, such as the Window object, the

Worksheet object, and the Workbook object.

19.3.2 AddComment Method

This method adds a Comment object (i.e., a comment) to the single-cell range. Its syntax is:


where Text is the text of the comment. For instance, the code:





rng As Range

c As Comment

rng = Range("B2")

c = rng.AddComment("This is a comment")

adds a comment to cell B2 with the text "This is a comment." Note that if RangeObject consists

of more than a single cell, a runtime error results.

19.3.3 Address Property (Read-Only String)

237 ®


The Address property returns the range reference of the Range object as a string. The syntax is:

RangeObject.Address(RowAbsolute, ColumnAbsolute, _

ReferenceStyle, External, RelativeTo)

RowAbsolute is set to True (the default) to return the row part of the reference as an absolute

reference. ColumnAbsolute is set to True (the default) to return the column part of the

reference as an absolute reference.

ReferenceStyle can be one of the XlReferenceStyle constants xlA1 or xlR1C1. The

default value is xlA1.

Set the External parameter to True to return an external reference—that is, a reference that is

qualified by the workbook and worksheet names and is thus valid outside the current worksheet.

The default value of False returns a reference that is not qualified and is therefore valid only

within the current worksheet.

Finally, the RelativeTo parameter is used when RowAbsolute and ColumnAbsolute are

False and ReferenceStyle is xlR1C1. In this case, we must include a reference point (a cell)

to use for the relative addresses. Let us consider some examples to help clarify this property:

Set rng = Range("B2")


' Returns

rng.Address(ReferenceStyle:=xlA1, _


' Returns



' Returns

rng.Address(RowAbsolute:=False, _

ColumnAbsolute:=False, ReferenceStyle:=xlA1) ' Returns

rng.Address(RowAbsolute:=False, _

ColumnAbsolute:=False, ReferenceStyle:=xlR1C1, _


' Returns

Set rng = Range("B2:D5")


' Returns


' Returns

rng.Address(RowAbsolute:=False, _

ColumnAbsolute:=False, ReferenceStyle:=xlA1) ' Returns

rng.Address(RowAbsolute:=False, _

ColumnAbsolute:=False, ReferenceStyle:=xlR1C1, _


' Returns










19.3.4 AutoFill Method

This important method performs an autofill on the cells in the range. Its syntax is:

RangeObject.AutoFill(Destination, Type)

Here Destination is the Range object whose cells are to be filled. The destination must include

the source range—that is, the range that contains the data to use for the autofill.

The optional Type parameter specifies the fill type. It can be one of the following

XlAutoFillType constants (note that the Excel documentation refers to a nonexistent

XlFillType enum):


Enum XlAutoFillType

xlFillDefault = 0

xlFillCopy = 1

xlFillSeries = 2

xlFillFormats = 3

xlFillValues = 4

xlFillDays = 5

xlFillWeekdays = 6

xlFillMonths = 7

xlFillYears = 8

xlLinearTrend = 9

xlGrowthTrend = 10

End Enum

If this argument is xlFillDefault or is omitted, Excel will attempt to select the most

appropriate fill type, based on the source data.

To illustrate, consider the code:

Range("A1:B1").AutoFill Range("A1:K1")

which autofills cells C1 through K1 using the source data in cells A1 and B1. If A1 contains 1 and

B1 contains 2, then this code will fill the destination cells with consecutive integers starting at 3

(in cell C1). Note that cells A1 and B1 are included in the destination range.

As another illustration, consider the worksheet in Figure 19-3, where cell B1 contains the formula:


Figure 19-3. Worksheet to autofill range B1:B5

The code:

Range("B1").AutoFill Range("B1:B5")

will produce the output shown in Figure 19-4.

Figure 19-4. Autofilling B1:B5 in Figure 19-3


We should mention one source of potential problems with the AutoFill method. Apparently, when

AutoFill is executed, the formula in the source cell is copied, with changes, to other cells.

However, the value of the source cell is also copied, but without changes. Thus, if autocalculation

is off, the formulas in the autofilled cells will be correct but the values will be incorrect. To fix

this, just invoke the Calculate method.

19.3.5 AutoFilter Method

The AutoFilter method has two syntaxes, corresponding to two distinct functions. The syntax:


simply toggles the display of the AutoFilter drop-down arrows for the columns that are involved

in the range.

The syntax:

RangeObject.AutoFilter(Field, Criteria1, Operator, Criteria2)

displays the AutoFilter arrows and filters a list using the AutoFilter feature.

The optional Field parameter is the offset (as an integer, counting from the left) of the field on

which the filter is based (the leftmost field is field one).

The optional Criteria1 parameter is the criteria (as a string). We can use "=" to find blank

fields, or "<>" to find nonblank fields. If this argument is omitted, the criteria is All. If Operator

(see the following example) is set to xlTop10Items, then Criteria1 specifies, as an integer,

the number of items to display (this number need not be equal to 10).

The Operator parameter can be one of the following XlAutoFilterOperator constants:

Enum XlAutoFilterOperator

xlAnd = 1

xlOr = 2

xlTop10Items = 3

xlBottom10Items = 4

xlTop10Percent = 5

xlBottom10Percent = 6

End Enum

If this parameter is set to xlAnd or xlOr, then we must use Criteria1 and Criteria2 to

construct the compound criteria.

To illustrate, consider the worksheet shown in Figure 19-5.

Figure 19-5. A worksheet before autofiltering


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