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 )
=A1:C3 + A5:C7
19.3.29 FormulaHidden Property (R/W Boolean)
This property returns or sets the Hidden state (True or False) for the formula in the cell to
which it is applied. This is equivalent to setting the Hidden check box in the Protection tab of the
Format Cells dialog.
Note that this is not the same as the Hidden property, which applies to ranges that consist of entire
rows (or entire columns) and determines whether or not those rows (or columns) are hidden from
19.3.30 HasFormula Property (Read-Only)
This property returns True if all cells in the range contain formulas; it returns False if none of
the cells in the range contains a formula and Null otherwise.
19.3.31 HorizontalAlignment Property
The HorizontalAlignment property returns or sets the horizontal alignment of all cells in the range.
The value can be one of the following constants:
xlHAlignRight = -4152
xlHAlignLeft = -4131
xlHAlignJustify = -4130
xlHAlignDistributed = -4117
'for Far East Excel
xlHAlignCenter = -4108
xlHAlignGeneral = 1
xlHAlignFill = 5
xlHAlignCenterAcrossSelection = 7
Note especially the xlHAlignCenterAcrossSelection constant, which is very useful for
aligning a title across multiple cells.
19.3.32 IndentLevel Property and InsertIndent Method
The IndentLevel property returns or sets the left indent for each cell in the range and can be any
integer between 0 and 15. All other settings cause an error. Presumably, indents are useful for
aligning the contents of cells or for formatting text.
For instance, to set the indent level of cell A1 to 10, we can write:
Range("A1").IndentLevel = 10
Unfortunately, the documentation does not specify how big an indent unit is, but we can still use
indent units in a relative way. Presumably, an indent level of 2 is twice that of an indent level of 1.
An alternative is to use the InsertIndent method, with the syntax:
where InsertAmount is an integer between 0 and 15. However, in this case, the InsertAmount
parameter specifies the amount to change the current indent for the range.
19.3.33 Insert Method
This method inserts a cell or range of cells into the worksheet, shifting existing cells to make room.
The syntax is:
where Shift can be one of the XlInsertShiftDirection constants:
xlShiftToRight = -4161
xlShiftDown = -4121
If the Shift argument is omitted, Excel will decide upon the shift direction based on the shape of
the range. (As with other cases when Excel will guess, I recommend against allowing it to do so.)
19.3.34 Locked Property
This property returns the Locked status of the cells in the range or can be used to lock the range.
The property returns Null if the range contains both locked and unlocked cells.
19.3.35 Merge-Related Methods and Properties
It is quite common to create a merged cell (that is, a single cell created by combining several
adjacent cells) for use as a title or heading, for instance.
The Merge method creates a merged cell from the specified range. The syntax is:
where Across is an optional Variant that should be set to True to merge the cells in each row of
the range into a single cell per row or False (the default) to merge all cells in all rows into a
single cell. Note that when the individual cells contain data, the merged cell will contain only the
data from the upper-left cell. Hence, the data in all other cells will be lost.
The UnMerge method separates a merged area into individual cells. Its syntax is:
Note that as long as RangeObject contains any of the cells within a merged range, even if it
does not contain all merged cells or if it contains additional cells not in the merged area, the
method will unmerge the merged range. Note that calling the UnMerge method on a range that
does not contain merged cells has no effect and does not produce a runtime error.
The MergeArea property applies only to ranges that consist of a single cell (otherwise an error
occurs). The property returns a Range object representing the merged range containing that cell
(or the cell itself if it is not part of a merged range).
The MergeCells property returns True if the specified range is contained within a merged range
of cells. The property returns Null if the specified range contains cells that are within a merged
range as well as cells that lie outside the merged range.
19.3.36 Next and Previous Properties
When applied to a Range object, the Next property returns the cell that would be made active by
key, although it does not actually select that cell. Thus, on an unprotected sheet, this
property returns the cell immediately to the right of the upper-left cell in the range. On
a protected sheet, this property returns the next unlocked cell.
Similarly, the Previous property emulates the Shift-Tab key by returning the appropriate cell (also
without selecting the cell).
19.3.37 NumberFormat Property
This property returns or sets the number-formatting string for the cells in the range. Note that the
property will return Null if the cells in the range do not all have the same number format.
One of the simplest ways to determine the desired formatting string is to record an Excel macro
and use the Format dialog. You can then inspect the macro code for the correct formatting string.
19.3.38 Parse Method
This method parses the data in a column (or portion thereof) and distributes the contents of the
range to fill adjacent columns. The syntax is:
where RangeObject can be no more than one column wide.
The ParseLine parameter is a string containing left and right brackets to indicate where the data
in the cells in the column should be split. For example, the string:
causes the Parse method to insert the first three characters from each cell into the first column of
the destination range, skip the fourth character, and then insert the next three characters into the
second column. Any additional characters (beyond the first six) are not included in the destination.
This makes the Parse method most useful for parsing fixed-length data (each cell has data of the
The Destination parameter is a Range object that represents the upper-left corner of the
destination range for the parsed data. If this argument is omitted, Excel will parse the data in place;
that is, it will use the source column as the first destination column.
19.3.39 PasteSpecial Method
This method pastes data from the Clipboard into the specified range. The syntax is:
RangeObject.PasteSpecial(Paste, Operation, SkipBlanks, Transpose)
The Paste parameter indicates what will be pasted and is one of the following XlPasteType
xlPasteValues = -4163
xlPasteComments = -4144
xlPasteFormulas = -4123
xlPasteFormats = -4122
xlPasteAll = -4104
xlPasteAllExceptBorders = 6
The optional Operation parameter specifies a paste operation and can be one of the following
xlPasteSpecialOperationNone = -4142
xlPasteSpecialOperationAdd = 2
xlPasteSpecialOperationSubtract = 3
xlPasteSpecialOperationMultiply = 4
xlPasteSpecialOperationDivide = 5
The SkipBlanks parameter should be set to True to skip pasting blank cells from the Clipboard.
To illustrate, suppose that the cell on the Clipboard that is destined to be pasted into cell D5 is
blank. If SkipBlanks is False (the default), then whatever is in D5 before the paste operation
will be overwritten when the blank cell is pasted, so D5 will then be empty. However, if
SkipBlank is True, the blank cell will not be pasted into D5 and so the contents of D5 will not
The optional Transpose parameter can be set to True to transpose rows and columns when the
range is pasted. The default value is False.
19.3.40 PrintOut Method
The PrintOut method prints a range. (This method applies to a host of other objects as well, such
as Worksheet, Workbook, and Chart.) The syntax is:
RangeObject.PrintOut(From, To, Copies, Preview, _
ActivePrinter, PrintToFile, Collate)
Note that all of the parameters to this method are optional.
The From parameter specifies the page number of the first page to print, and the To parameter
specifies the last page to print. If omitted, the entire object (range, worksheet, etc.) is printed.
The Copies parameter specifies the number of copies to print. The default is 1.
Set Preview to True to invoke print preview rather than printing immediately. The default is
False. ActivePrinter sets the name of the active printer.
Setting PrintToFile to True causes Excel to print to a file. Excel will prompt the user for the
name of the output file. (Unfortunately, there is no way to specify the name of the output file in
The Collate parameter should be set to True to collate multiple multipage copies.
19.3.41 PrintPreview Method
This method invokes Excel's print preview feature for the given range (this method applies to the
same list of objects as the PrintOut method). Its syntax is:
19.3.42 Replace Method
This method finds and replaces specified data in all cells in a range. It has no effect on the
selection or the active cell. The syntax is:
RangeObject.Replace(What, Replacement, LookAt, _
SearchOrder, MatchCase, MatchByte)
The What parameter is the data to search for, and the Replacement parameter is the
replacement data. These data can be strings or any other valid Excel data types (numbers, dates,
The LookAt parameter is one of the following constants that determines whether the What value
must match the cell's entire contents or just any part of the cell's contents:
xlWhole = 1
xlPart = 2
The SearchOrder parameter is one of the following XlSearchOrder constants:
xlByRows = 1
xlByColumns = 2
The MatchCase parameter should be set to True to do a case-sensitive search (the default is
False). The MatchByte parameter is used only in the Far East version of Microsoft Excel. See
the help documentation for details.
Note that the values of the LookAt, SearchOrder, MatchCase, and MatchByte parameters
are saved each time the Find method is invoked and then reused for the next call to this method.
Note also that setting these arguments changes the corresponding settings in Excel's Find dialog
box, and conversely, changing the settings in the Find dialog box changes the values of these
parameters. This implies that we cannot rely on the values of these parameters, since the user may
have changed them through the Find dialog box. Hence, it is important to specify each of these
arguments for each call to the Find method.
If the contents of the What argument are found at least once, the Replace method returns True.
19.3.43 Select Method
This method selects the given range. Actually, the Select method applies to a whopping 81
different Excel objects. For the Range object, its syntax is:
Note that this method selects a range of cells, whereas the Activate method activates a single cell.
19.3.44 ShrinkToFit Property
This property can be set to True to tell Excel to shrink the font size of all text in the range so that
the text fits the available column width. It also returns True if ShrinkToFit is set for all cells in
the range, False if it is turned off for all cells in the range, or Null if some cells have
ShrinkToFit turned on and others have ShrinkToFit turned off.
19.3.45 Sort Method
This method sorts a range or the current region when the specified range contains only one cell. It
can also be used to sort a pivot table. The syntax is:
RangeObject.Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, _
Header, OrderCustom, MatchCase, Orientation, SortMethod, _
IgnoreControlCharacters, IgnoreDiacritics, IgnoreKashida)
Sorting can take place based on up to three keys, denoted by Key1, Key2, and Key3. These
parameters can be expressed as text (a range name) or a Range object. The corresponding Order
parameter can be set to one of the following values:
xlAscending = 1
xlDescending = 2
The optional Type parameter is used only when sorting pivot tables.
The optional Header parameter specifies whether the first row contains headers, in which case
they are not included in the sort. The Header parameter can be one of the following values:
xlGuess = 0
xlYes = 1
xlNo = 2
The optional OrderCustom parameter is an integer offset into the list of custom sort orders.
However, Microsoft seems not to have documented this further, so it seems best to simply omit
this argument, in which case it is assumed to be Normal (which sounds good).
The optional MatchCase parameter should be set to True to do a case-sensitive sort and False
(the default) to do a sort that is not case-sensitive. For instance, suppose that cell A1 contains the
text "AAA" and cell A2 contains the text "aaa." The code:
Range("A1:A2").Sort Key1:=Cells(1, 1), MatchCase:=True
will swap the contents of these two cells, but the code:
Range("A1:A2").Sort Key1:=Cells(1, 1), MatchCase:=False
The optional Orientation parameter determines whether the sort is done by row or by column.
It can assume either of the values in the following enum:
xlSortColumns = 1
xlSortRows = 2
Range("A1:B2").Sort Key1:=Rows(1), Orientation:=xlSortColumns
sorts the columns in the range A1:B2 using the first row for the sort key.
The rest of the parameters are not used in the U.S. English version of Excel. The SortMethod
parameter is not documented, but it has a default value xlPinYin, whatever that means.
19.3.46 SpecialCells Method
This method returns a Range object that represents all the cells that match a specified type and
value. The syntax is:
The Type parameter specifies the type of cells to include from RangeObject. It can be one of
the following XlCellType constants:
xlCellTypeComments = -4144
xlCellTypeFormulas = -4123
xlCellTypeConstants = 2
xlCellTypeBlanks = 4
xlCellTypeLastCell = 11
xlCellTypeVisible = 12
'Cells with comments
'Cells with formulas
'Cells with constants
'Last cell in range
'All visible cells
For instance, the code:
selects all blank cells in the range A1:D10.
The optional Value parameter applies when the Type parameter is either
xlCellTypeConstants or xlCellTypeFormulas and identifies more specifically the type
of cell to return. In these cases, the Value parameter can be set to one of, or a sum of, the
xlNumbers = 1
xlTextValues = 2
xlLogical = 4
xlErrors = 16
For instance, the code:
selects only the cells with text (as opposed to numbers) within the range A1:D10.
19.3.47 TextToColumns Method
This method parses a column (or columns) of cells that contain text into several columns. The
RangeObject.TextToColumns(Destination, DataType, _
TextQualifier, ConsecutiveDelimiter, Tab, Semicolon, _
Comma, Space, Other, OtherChar, FieldInfo)
Note that all of the parameters to this method are optional.
The Destination parameter is a Range object that specifies where to put the results of the
conversion. If the Range object represents more than a single cell, then the starting point for the
destination is the upper-left cell in that range.
The DataType parameter specifies the format of the text to be split into columns. It can be one of
the following XlTextParsingType constants:
xlDelimited = 1
xlFixedWidth = 2
The TextQualifier parameter is the text qualifier. It can be one of the following
xlTextQualifierNone = -4142
xlTextQualifierDoubleQuote = 1
xlTextQualifierSingleQuote = 2
The ConsecutiveDelimiter parameter should be set to True to have Excel consider
consecutive delimiters as one delimiter. The default value is False.
There are several parameters that require that the DataType be xlDelimited and, when set to
True, indicate that Excel should use the corresponding character as the text delimiter. They are
described in the following list (all default values are False):
Set to True to use the tab character as delimiter.
Set to True to use a semicolon as delimiter.
Set to True to use a comma as delimiter.
Set to True to use a space as delimiter.
Set to True to use a character that is specified by the OtherChar argument as delimiter.
When Other is True, OtherChar specifies the delimiter character. If OtherChar contains
more than one character, only the first character is used.
The FieldInfo parameter is an array containing parse information for the individual source
columns. The interpretation of FieldInfo depends on the value of DataType.
When DataType is xlDelimited, the FieldInfo argument should be an array whose size is
the same as (or smaller than—see Table 19-3) the number of columns of converted data. The first
element of a two-element array is the column number (starting with the number 1), and the second
element is one of the numbers in Table 19-3 that specifies how the column is parsed.
Table 19-3. FieldInfo Values for xlDelimited Text
Skip the column
If a two-element array for a given column is missing, then the column is parsed with the General
setting. For instance, the following value for FieldInfo causes the first column to be parsed as
text and the third column to be skipped:
Array(Array(1, 2), Array(3, 9))
All other columns will be parsed as general data.
To illustrate, consider the sheet shown in Figure 19-15. The code:
ConsecutiveDelimiter:=True, Comma:=True, _
FieldInfo:=Array(Array(1, 2), Array(2, 3))
produces the second and third columns of Figure 19-15. Note that the cells in column C are
formatted as dates.
Figure 19-15. A worksheet with text to be parsed in A1:A3
On the other hand, if DataType is xlFixedWidth, the first element of each two-element array
specifies the starting character position in the column (0 being the first character) and the second
element specifies the parse option (1-9) for the resulting column, as described previously.
To illustrate, consider the worksheet in Figure 19-16. The code:
FieldInfo:=Array(Array(0, 2), _
Array(1, 9), Array(2, 2), Array(5, 9), _
parses the first column of Figure 19-16 into the remaining columns. (Note how we included arrays
to skip the hyphens.)
Figure 19-16. A worksheet with fixed-width data to be parsed in A1:A3
19.3.48 Value Property
The Value property returns the value of the specified cell. If the cell is empty, Value returns an
empty string. This can be tested in either of the following ways:
If Range("A1") = "" Then . . .
If IsEmpty(Range("A1")) Then . . .
If the Range object contains more than one cell, the Value property returns a two-dimensional
array. For instance, referring to Figure 19-16, the code:
Dim v As Variant
v = Range("A1:A3").Value