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 )
A Name object represents a defined name for a range of cells. There are two types of names in
Excel: built-in names such as Print_Area and custom names created by the user or by code.
Name objects are kept in several Names collections. There is a Names collection for the
Application object, as well as Names collections for each Workbook and Worksheet object.
There are a variety of ways to create a new Name object. We can add a Name object to a Names
collection by calling the collection's Add method or we can use the CreateNames method of the
Range object (discussed in Chapter 19).
For instance, the following code creates a Name object that refers to a range on Sheet1 of Book1.
The Name object is added to the workbook's Names collection, but not to Sheet1's Names
Note the use of a sheet qualifier in the RefersTo parameter and the specification of an absolute
address. If the absolute operator ($) is not used, the range will be defined relative to the active cell.
The following code adds a Name object to the Names collection of Sheet1 and Sheet2:
.Names.Add Name:="WkSheet1Name", _
Names.Add Name:="WkSheet2Name", _
Note that this code will also add the Name objects to the workbook's Names collection.
The following code sets the font for the range WkSheet1Name to boldface:
Sheet1.Names("WkSheet1Name").RefersToRange.Font.Bold = True
Note that there is no Names collection for a given Range object, even though a Range object can
have more than one name. The best we can do is retrieve the first name for a range object by using
the Name property (see the discussion in Chapter 19).
Let us review some of the properties and methods of the Name object:
This method, whose syntax is:
deletes the Name object from the Names collections in which it resides. It does not delete
the actual range.
This property returns or sets the name of the Name object.
This property returns or sets the formula that defines a named range, in A1-style notation,
beginning with an equal sign.
This property returns or sets the formula that defines a named range, in R1C1-style
notation, beginning with an equal sign.
This property returns the Range object referred to by the named range. It is read-only.
(See the previous example code.)
This property also returns or sets the formula that defines a named range, in A1-style
notation, beginning with an equal sign. Thus, it is equivalent to the RefersTo property.
This property returns or sets the visibility of the named range.
16.2.2 The Windows Collection and Window Objects
Of course, a Window object represents an Excel window. The Windows collection of the
Application object is the collection of Window objects for all currently open windows in the
currently running version of Excel. (Similarly, the Windows collection for a Workbook object
contains only the windows in the workbook.)
The Arrange method of the Windows collection is used to arrange the current windows. The
ActiveWorkbook, SyncHorizontal, SyncVertical)
The optional ArrangeStyle parameter can be one of the following XlArrangeStyle
xlArrangeStyleVertical = -4166
xlArrangeStyleHorizontal = -4128
xlArrangeStyleTiled = 1
xlArrangeStyleCascade = 7
We can set the ActiveWorkbook parameter to True to arrange only the visible windows of the
active workbook. The default value is False, in which case all windows are arranged.
When ActiveWorkbook is True, the remaining parameters are evaluated (otherwise they are
ignored). SyncHorizontal can be set to True to synchronize the horizontal scrolling windows
of the active workbook. In other words, all windows scroll at the same time when one window is
scrolled horizontally. The default value is False. Similarly, the SyncVertical parameter
specifies vertical scrolling synchronization. Thus, the following code tiles the visible windows and
enables horizontal scrolling synchronization:
To create a new window, we use the NewWindow method of the Workbook object, as in:
in which case a copy of the active window is created. This method also applies to an existing
Window object and creates a copy of the window to which it is applied.
The Windows collection has a special property with respect to indexing, namely, the active
window is always:
The 58 members of the Window object are shown in Table 16-6.
Table 16-6. Members of the Window object
16.2.3 The WorksheetFunction Object
The WorksheetFunction object is returned by the WorksheetFunction property of the Application
object. The sole purpose of the WorksheetFunction object is to provide access to Excel's
worksheet functions. For instance, the following code illustrates the use of the WorksheetFunction
object to access Excel's Min function:
Dim rng As Range
Dim rMin As Single
Set rng = Worksheets("Sheet1").Range("A1:D10")
rMin = Application.WorksheetFunction.Min(rng)