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 )

The code:

Range("A1:B5").AutoFilter 2, "1997", xlOr, "1998"

will filter the range A1:B5 to show only those rows in the range for either the year 1997 or 1998.

Note that it has no effect on the remaining rows of the worksheet. Hence, the result will be the

worksheet in Figure 19-5 with rows 3 and 4 missing.

Recall that the AutoFilterMode property of the Worksheet object is True if the AutoFilter dropdown arrows are currently displayed on the worksheet. Note that we can set this property to

False to remove the arrows, but we cannot set it to True. (To display the AutoFilter arrows, use

the AutoFilter method.)

Recall also that the FilterMode property is True if the worksheet is in filter mode. Thus, for

instance, if the AutoFilter arrows are displayed but no filtering has taken place, then

AutoFilterMode is True, whereas FilterMode is False. Once filtering is actually performed, then

FilterMode is True.

19.3.6 AutoFit Method

This method changes the width of the columns or the height of the rows (depending upon the type

of range) to obtain the best fit for the range's contents. The syntax is:

RangeObject.AutoFit

where RangeObject refers to a Range object that consists of either one or more rows or one or

more columns (but not both). Otherwise, the method generates an error. If the range consists of

columns, then the column width is adjusted. If the range consists of rows, then the row height is

adjusted.

19.3.7 AutoFormat Method

This method automatically formats a range using a predefined format. The syntax is:

RangeObject.AutoFormat(Format, Number, Font, _

Alignment, Border, Pattern, Width)

All parameters of this method are optional. The Format parameter can be one of the following

XlRangeAutoFormat constants:

241

Enum XlRangeAutoFormat

xlRangeAutoFormat3DEffects1 = 13

xlRangeAutoFormat3DEffects2 = 14

xlRangeAutoFormatAccounting1 = 4

xlRangeAutoFormatAccounting2 = 5

xlRangeAutoFormatAccounting3 = 6

xlRangeAutoFormatAccounting4 = 17

xlRangeAutoFormatClassic1 = 1

xlRangeAutoFormatClassic2 = 2

xlRangeAutoFormatClassic3 = 3

xlRangeAutoFormatClassicPivotTable = 31

xlRangeAutoFormatColor1 = 7

xlRangeAutoFormatColor2 = 8

xlRangeAutoFormatColor3 = 9

xlRangeAutoFormatList1 = 10

xlRangeAutoFormatList2 = 11

xlRangeAutoFormatList3 = 12

xlRangeAutoFormatLocalFormat1 = 15

xlRangeAutoFormatLocalFormat2 = 16

xlRangeAutoFormatLocalFormat3 = 19

xlRangeAutoFormatLocalFormat4 = 20

xlRangeAutoFormatNone = -4142

xlRangeAutoFormatPTNone = 42

xlRangeAutoFormatReport1 = 21

xlRangeAutoFormatReport10 = 30

xlRangeAutoFormatReport2 = 22

xlRangeAutoFormatReport3 = 23

xlRangeAutoFormatReport4 = 24

xlRangeAutoFormatReport5 = 25

xlRangeAutoFormatReport6 = 26

xlRangeAutoFormatReport7 = 27

xlRangeAutoFormatReport8 = 28

xlRangeAutoFormatReport9 = 29

xlRangeAutoFormatSimple = -4154

xlRangeAutoFormatTable1 = 32

xlRangeAutoFormatTable10 = 41

xlRangeAutoFormatTable2 = 33

xlRangeAutoFormatTable3 = 34

xlRangeAutoFormatTable4 = 35

xlRangeAutoFormatTable5 = 36

xlRangeAutoFormatTable6 = 37

xlRangeAutoFormatTable7 = 38

xlRangeAutoFormatTable8 = 39

xlRangeAutoFormatTable9 = 40

End Enum

Note that the constants marked as not used are not used in the U.S. English version of Excel.

The other parameters are Boolean and should be set to True (the default values) to include the

corresponding format feature, as follows:

Number

Include number formats

Font

Include font formats

242

Alignment

Include alignment

Border

Include border formats

Pattern

Include pattern formats

Width

Include column width and row height in the autoformat

Note that if the range is a single cell, the AutoFormat method also formats the current region

containing the cell. (The CurrentRegion property and the current region are discussed in detail

later in this section.) Put another way, the following two statements are equivalent:

Cells("A1").AutoFormat

Cells("A1").CurrentRegion.AutoFormat

19.3.8 BorderAround Method

This method adds a border to a range and optionally sets the Color, LineStyle, and Weight

properties for the border. The syntax is:

RangeObject.BorderAround(LineStyle, Weight, ColorIndex, Color)

The LineStyle parameter can be one of the following XlLineStyle constants (note that the

Excel documentation refers to a nonexistent XlBorderLineStyle enum):

Enum XlLineStyle

xlLineStyleNone = -4142

xlDouble = -4119

xlDot = -4118

xlDash = -4115

xlContinuous = 1

xlDashDot = 4

xlDashDotDot = 5

xlSlantDashDot = 13

End Enum

' the default

The optional Weight parameter specifies the border weight and can be one of the following

XlBorderWeight constants:

Enum XlBorderWeight

xlMedium = -4138

xlHairline = 1

xlThin = 2

xlThick = 4

End Enum

' the default

Note that the Weight property is ignored unless the LineStyle is xlContinuous or omitted.

243

The optional ColorIndex parameter specifies the border color, either as an index into the current

color palette or as one of the following XlColorIndex constants:

Enum XlColorIndex

xlColorIndexNone = -4142

xlColorIndexAutomatic = -4105

End Enum

The optional Color parameter also specifies the border color as an RGB value. Note that you

should specify at most one of the color parameters.

The technique for clearing a border is a bit unexpected. For instance, suppose we have set a border

with:

rng.BorderAround LineStyle:=xlDash

To clear this border, we might naturally try:

rng.BorderAround LineStyle:=xlLineStyleNone

but this does nothing. Instead, we must write:

rng.Borders.LineStyle = xlLineStyleNone

which clears the borders around each cell in the range separately.

19.3.9 Calculate Method

This method (which also applies to the Workbook and Worksheet objects) calculates all cells in

the specified range. For instance, the code:

Worksheets(1).Rows(2).Calculate

will calculate all of the cells in the second row of the first worksheet.

19.3.10 Clear Methods

Excel has several clear methods. In particular, the Clear method clears all contents, formulas, and

formatting from the cells in the given range. The ClearContents method clears only the contents

(values and/or formulas) from the cells in the range and leaves the formatting intact. The

ClearFormats method clears only the formatting from the cells in the range.

19.3.11 ColumnDifferences and RowDifferences Methods

The ColumnDifferences method returns a Range object that represents all the cells in the range

whose contents are different from certain comparison cells (there is one comparison cell in each

column). The syntax is:

RangeObject.ColumnDifferences(ComparisonCell)

where ComparisonCell is a range object that represents a single cell. The purpose of

ComparisonCell is simply to identify the row whose cells contain the comparison values.

To illustrate, consider the following code, whose results are shown in Figure 19-6:

244

Dim rng As Range, rng2 As Range

Set rng = Range("A1:D6")

Set rng2 = _

rng.ColumnDifferences(Comparison:=Range("A1"))

rng2.Select

Figure 19-6. The result of the ColumnDifferences method

Since the ComparisonCell is cell A1, the first cell of each column in the range contains the

comparison value. Thus, the cells that do not contain an "x" are selected in column A, the

nonblank cells are selected in column B, the cells that do not contain a "y" are selected in column

C and the cells that do not contain an "a" are selected in column D.

The RowDifferences method is the analog for rows of the ColumnDifferences method.

19.3.12 ColumnWidth and RowHeight Properties

The ColumnWidth property returns or sets the width of the columns in the specified range.

The return value is in units, each of which equals the width of one character in the Normal style.

For proportional fonts, the width of the character "0" (zero) is used.

Note that if the columns in the range do not all have the same width, the ColumnWidth property

returns Null.

The RowHeight property returns the height of all the rows in the range, measured in points. Note

that if the rows in the range do not all have the same height, the RowHeight property returns Null.

19.3.13 Width, Height, Top, and Left Properties

These properties return values for the entire range, in points. For instance, the Top property

returns the distance, in points, from the top of row 1 to the top of the first (leftmost) area of the

range.

Note that when applied to a column, the Width property returns the width, in points, of the column.

However, the relationship between Width and ColumnWidth can seem a bit strange at first.

For instance, the following code shows that a column of ColumnWidth 1 has Width 9.6 but a

column of ColumnWidth 2 has Width 15. (In my case, the Normal style is 10 point Arial.)

However, if the ColumnWidth property really measures the width of a column in units and the

Width property really measures the width of the same column in points, then doubling one of

these properties should double the other!

Columns("A").ColumnWidth = 1

MsgBox Columns("A").Width

' Displays 9.6

245

Columns("A").ColumnWidth = 2

MsgBox Columns("A").Width

' Displays 15

Columns("A").ColumnWidth = 10

MsgBox Columns("A").Width

' Displays 58.2

Fortunately, a little high-school algebra reveals the truth here. It appears that the Width property

includes padding on the far right and the far left of the entire group of characters (next to the

column boundaries). To support this conclusion, let's do a little algebra, which you can skip if it

upsets you.

Assume for a moment that the Width property includes not just the sum of the widths of the

ColumnWidth characters, but also an additional p points of padding on each side of the entire

group of characters. Thus, the formula for Width is:

Width = 2*p + ColumnWidth*w

where w is the true width of a single "0" character, in points. Thus, plugging in the values from the

first two examples in the previous code gives:

9.6 = 2*p + 1*w

15 = 2*p + 2*w

Subtracting the first equation from the second gives:

5.4 = w

Substituting this into the first equation and solving for p gives:

p = 2.1

Thus, the formula for a Normal style of 10 point Arial is:

Width = 4.2 + ColumnWidth*5.4

Now, for a ColumnWidth of 10, this gives:

Width = 4.2 + 10*5.4 = 58.2

Eureka! (Check the third example in the previous code.)

Thus, we have verified (but not really proved) that the Width property measures not just the width

of each character but includes some padding on the sides of the column—in this case 2.1 points of

padding on each side.

19.3.14 Consolidate Method

This method combines (or consolidates) data from multiple ranges (perhaps on multiple

worksheets) into a single range on a single worksheet. Its syntax is:

RangeObject.Consolidate(Sources, Function, _

TopRow, LeftColumn, CreateLinks)

Sources is the source of the consolidation. It must be an array of references in R1C1-style

notation. The references must include the full path of the ranges to be consolidated. (See the

following example.)

246

Tải bản đầy đủ (.pdf) (490 trang)