Tải bản đầy đủ - 0 (trang)
Figure 19-6. The result of the ColumnDifferences method

Figure 19-6. The result of the ColumnDifferences method

Tải bản đầy đủ - 0trang

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



Function is the function used to combine the data. It can be one of the following

XlConsolidationFunction constants. (The default value is xlAverage.)

Enum XlConsolidationFunction

xlAverage = -4106

xlCount = -4112

xlCountNums = -4113

xlMax = -4136

xlMin = -4139

xlProduct = -4149

xlStDev = -4155

xlStDevP = -4156

xlSum = -4157

xlUnknown = 1000

xlVar = -4164

xlVarP = -4165

End Enum

TopRow should be set to True to consolidate the data based on column titles in the top row of the

consolidation ranges. Set the parameter to False (the default) to consolidate data by position. In

other words, if TopRow is True, Excel will combine columns with the same heading, even if they



are not in the same position.



AM

FL

Y



LeftColumn should be set to True to consolidate the data based on row titles in the left column

of the consolidation ranges. Set the parameter to False (the default) to consolidate data by

position.

CreateLinks should be set to True to have the consolidation use worksheet links. Set the

parameter to False (the default) to have the consolidation copy the data.



TE



To illustrate, consider the worksheets in Figure 19-7 and Figure 19-8 (note the order of the

columns).

Figure 19-7. Sheet2 before consolidation



Figure 19-8. Sheet3 before consolidation



The code:

Worksheets("Sheet1").Range("A1").Consolidate _

Sources:=Array("Sheet2!R1C1:R3C3", _



247 ®

Team-Fly



"Sheet3!R1C1:R3C3"), Function:=xlSum



will produce the results shown in Figure 19-9 (on Sheet1).

Figure 19-9. Sheet1 after consolidation with TopRow set to False



On the other hand, setting the TopRow property to True:

Worksheets("Sheet1").Range("A1").Consolidate _

Sources:=Array("Sheet2!R1C1:R3C3", _

"Sheet3!R1C1:R3C3"), Function:=xlSum, _

TopRow:=True



produces the results shown in Figure 19-10, since the data is combined based on the names in the

first row.

Figure 19-10. Sheet1 with TopRow set to True



19.3.15 Copy and Cut Methods

As applied to the Range object, the Copy method has the syntax:

RangeObject.Copy(Destination)



where Destination is a Range object that specifies the new range to which the specified range

will be copied. If this argument is omitted, Excel will copy the range to the Clipboard. For

instance, the code:

Range("A1:C3").Copy Range("D5")



copies the range A1:C3 to a range of like size whose upper-left corner is cell D5. Note that the

same rules apply here as when copying using Excel's user interface. In particular, if the destination

is more than a single cell, then it must have the same dimensions as the source range or else an

error will occur.

The Cut method has similar syntax:

RangeObject.Cut(Destination)



and cuts the range rather than copying it.



248



19.3.16 CopyFromRecordset Method

For those readers familiar with DAO, CopyFromRecordset is a very powerful method that copies

the contents of a DAO Recordset object onto a worksheet, beginning at the upper-left corner of the

specified range. Note that if the Recordset object contains fields with OLE objects in them, this

method fails.

To illustrate, consider the following code, which requires that a reference to Microsoft DAO is set

in the References dialog box in the Excel VBA Tools menu:

Dim rs As Recordset

Set rs = _

DBEngine.OpenDatabase("d:\excel\excel.mdb"). _

OpenRecordset("Objects")

Range("A1").CopyFromRecordset(rs, 10, 10)



This code opens an Access database named d:\excel\excel.mdb, creates a recordset based on the

table named Objects, and then copies the first 10 columns of the first 10 rows of the recordset to

the current worksheet, starting at cell A1.

Note that, in general, copying begins at the current row of the Recordset object (which in our

example is the first row, since we opened the recordset anew).



19.3.17 CreateNames Method

This method creates range names based on text labels in specified cells. The syntax is:

RangeObject.CreateNames(Top, Left, Bottom, Right)



The parameters are optional and have the default value of False. If one of the parameters is set to

True, then the corresponding row (Top or Bottom) or column (Left or Right) is used to

supply the names for the named ranges. If all of the parameters are False, then Excel tries to

guess the location of the names. (I would generally advise against letting an application guess at

anything.)

To illustrate, the following code, when applied to the sheet in Figure 19-8, will define three named

ranges:

Range("A1:C3").CreateNames Top:=True



For instance, the range A2:A3 will be named John.



19.3.18 CurrentRegion Property

This useful property returns a Range object that represents the current region, which is the region

bound by the closest empty rows and columns. To illustrate, the following code, when applied to

the sheet in Figure 19-11, selects the rectangular region A2:C4:

ActiveCell.CurrentRegion.Select



Figure 19-11. Illustrating CurrentRegion



249



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Figure 19-6. The result of the ColumnDifferences method

Tải bản đầy đủ ngay(0 tr)

×