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.3.21 Precedents and DirectPrecedents Properties
These properties work just like the Dependents and DirectDependents properties, but in the
reverse direction. For instance, referring to Figure 19-12, the line:
selects the cells B1 and A1, whereas the line:
selects the cell B1.
19.3.22 End Property
This property returns a Range object that represents the cell at the "end" of the region that contains
the source range by mimicking a keystroke combination (see the following code). The syntax is:
where RangeObject should be a reference to a single cell and Direction is one of the
following constants. (The keystroke combination is also given in the following code.)
xlUp = -4162
xlToRight = -4161
xlToLeft = -4159
xlDown = -4121
Thus, for instance, the code:
selects the rightmost cell in Row 4 for which all cells between that cell and cell C4 are nonempty
(that is, the cell immediately to the left of the first empty cell in row 4 following cell C4).
19.3.23 EntireColumn and EntireRow Properties
The EntireColumn property returns a Range object that represents the column or columns that
contain the specified range. The EntireRow property returns a Range object that represents the
row or rows that contain the specified range.
For instance, the code:
selects the first three rows of the current worksheet.
19.3.24 Fill Methods
The Excel object model has four Fill methods: FillDown, FillUp, FillLeft, and FillRight. As
expected, these methods work similarly, so we will describe only FillDown.
The FillDown method fills down from the top cell or cells in the specified range to the bottom of
the range. The contents, formulas, and formatting of the cell or cells in the top row of a range are
copied into the rest of the rows in the range. The syntax is:
For instance, the code:
will duplicate the values of cells B3 through B5 in cells C3 through C5 and D3 through D5.
19.3.25 Find Method
The Find method returns the first cell in a given range that satisfies a criterion. Note that the Find
method returns Nothing if no match is found. In any case, it does not affect the selection or the
The syntax of the Find method is:
RangeObject.Find(What, After, LookIn, LookAt, _
SearchOrder, SearchDirection, MatchCase, MatchByte)
Note that all of the parameters except What are optional.
The What parameter is the data to search for and can be a string or any other valid Excel data type
(number, date, etc.).
The After parameter is the cell after which the search should begin. (This would be the active
cell when doing a search from the user interface.) Thus, the cell referred to by After is the last
cell searched. If the After argument is omitted, the search starts after the cell in the upper-left
corner of the range.
The LookIn parameter is one of the following constants:
xlValues = -4163
xlComments = -4144
xlFormulas = -4123
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 SearchDirection parameter is one of the following XlSearchDirection constants:
xlNext = 1
xlPrevious = 2
The MatchCase parameter should be set to True to do a case-sensitive search; otherwise, the
search will be case-insensitive. (The MatchByte parameter is used only in the Far East version of
Microsoft Excel. See the help documentation for details.)
There are several things to note about the Find method:
The values of the LookIn , LookAt, SearchOrder, MatchCase, and MatchByte
parameters (but not the SearchDirection parameter) are saved each time the Find
method is invoked and are 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.
The FindNext and FindPrevious methods (described in the next section) can be used to
repeat a search.
When a search reaches the end of the specified search range, it wraps around to the
beginning of the range. If you do not want this behavior, consider using a different range.
To find cells that match more complicated search criteria, such as those involving
wildcard matches, we must use a more manual approach, such as cycling through the cells
in the range with a For Each loop and using the Like operator. For instance, the
following code searches for all cells in the range A1:C5 whose contents begin with an
"A" and sets the font for these cells to bold (note the use of the evaluation operator to
denote the range A1:C5):
Dim c As Range
For Each c In [A1:C5]
If c.Value Like "A*" Then
c.Font.Bold = True
19.3.26 FindNext and FindPrevious Methods
The FindNext method continues a search that was started with the Find method, returning the next
cell that matches the criteria. The syntax is:
The After parameter must be specified or the search will begin at the upper-left corner of the
range. Thus, FindNext is the same as Find, except that it uses all of the parameters (except After)
that were set by the previous use of the Find method.
To continue the search from the last cell found, use that cell as the After argument. For instance,
the following code searches for all cells in the top row that contain the value 0 and removes the
Dim c As Range
Dim sFirstHit As String
' Address of first hit
Set c = .Find(0, LookIn:=xlValues)
If Not c Is Nothing Then
sFirstHit = c.Address
' Change cell contents
c.Value = ""
' find next cell
Set c = .FindNext(c)
Loop While Not c Is Nothing
The FindPrevious method has the syntax:
and works just like the FindNext method, but searches backward in the range starting with the cell
before the cell referred to by the Before parameter (with wrap around from the beginning of the
range to the end).
19.3.27 Formula and FormulaR1C1 Properties
The Formula property returns or sets the formula or value for each cell in the range. The formula
must be expressed in A1-style notation, and must include a leading equal sign.
For instance, the line:
Range("A1").Formula = "=Sum(A2:A3)"
sets the formula in cell A1. The line:
Range("A1:C1").Formula = "=Sum(A2:A3)"
places the formula in cells A1:C1, but because the formula uses relative references, these
references will be altered as usual. If we want to put the exact same formula in each cell, we must
use an array, as in:
Range("A1:C1").Formula = _
Array("=Sum(A2:A3)", "=Sum(A2:A3)", "=Sum(A2:A3)")
We can also return an array using the Formula property. To illustrate, consider the worksheet in
Figure 19-13. The code:
Dim a As Variant
a = Range("A1:C2").Formula
sets the Variant variable a to an array, so that, for instance, a(2,3) = 7. Note that the Formula
property returns a Variant, so that a must be declared as a Variant.
Figure 19-13. Illustrating the Formula property
If a cell contains a constant, the Formula property returns that constant. We can also assign a
constant to a cell by writing, for example:
Range("A1").Formula = 1
If the cell is empty, then the Formula property returns an empty string. If the cell contains a
formula, then the Formula method returns the formula as a string, as it would be displayed in the
formula bar (including the equal sign).
If we set the Formula property (or the Value property) of a cell to a date, then Excel checks to see
whether that cell is already formatted with one of the date or time formats. If not, Excel uses the
default short date format.
The FormulaR1C1 property is the analog to the Formula property but accepts and returns formulas
in R1C1 style.
19.3.28 FormulaArray Property
The FormulaArray property returns or sets an array formula, which must be in R1C1 style, for a
range. To illustrate, consider the worksheet shown in Figure 19-14. The code:
Range("A9:C11").FormulaArray = "=A1:C3 + A5:C7"
produced the values in cells A9:C11 in Figure 19-14. The formula on the left says to add the
contents of each cell in the uppermost 3-by-3 array to the corresponding cell in the middle 3-by-3
array, and place the result in the corresponding cell in the lower 3-by-3 array.
Figure 19-14. Illustrating the FormulaArray property
Note also that the code:
prints the array formula: