Tải bản đầy đủ - 0 (trang)
Figure 16-4. The Excel Application object and its children

Figure 16-4. The Excel Application object and its children

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

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

collection:

Workbooks("Book1.xls").Names.Add Name:="WkBkName"

RefersTo:="=Sheet1!$A$1:$B$1"



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:

Workbooks("Book1.xls").Worksheets("Sheet1") _

.Names.Add Name:="WkSheet1Name", _

RefersTo:="=Sheet1!$A$1:$B$1"

Workbooks("Book1.xls").Worksheets("Sheet2"). _

Names.Add Name:="WkSheet2Name", _

RefersTo:="=Sheet2!$A$1:$B$1"



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:

Delete method

This method, whose syntax is:

NameObject.Delete



deletes the Name object from the Names collections in which it resides. It does not delete

the actual range.

Name property

This property returns or sets the name of the Name object.



190



RefersTo property

This property returns or sets the formula that defines a named range, in A1-style notation,

beginning with an equal sign.

RefersToR1C1 property

This property returns or sets the formula that defines a named range, in R1C1-style

notation, beginning with an equal sign.

RefersToRange property

This property returns the Range object referred to by the named range. It is read-only.

(See the previous example code.)

Value property

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.

Visible 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

syntax is:

WindowsObject.Arrange(ArrangeStyle, _

ActiveWorkbook, SyncHorizontal, SyncVertical)



The optional ArrangeStyle parameter can be one of the following XlArrangeStyle

constants:

Enum XlArrangeStyle

xlArrangeStyleVertical = -4166

xlArrangeStyleHorizontal = -4128

xlArrangeStyleTiled = 1

xlArrangeStyleCascade = 7

End Enum



' Default



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



191



specifies vertical scrolling synchronization. Thus, the following code tiles the visible windows and

enables horizontal scrolling synchronization:

ActiveWorkbook.Windows.Arrange _

ArrangeStyle:=xlArrangeStyleTiled, _

SyncHorizontal:=True



To create a new window, we use the NewWindow method of the Workbook object, as in:

ThisWorkbook.NewWindow



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:

Windows(1)



The 58 members of the Window object are shown in Table 16-6.

Table 16-6. Members of the Window object

_DisplayRightToLeft

EnableResize

ScrollWorkbookTabs

Activate

FreezePanes

SelectedSheets

ActivateNext

GridlineColor

Selection

ActivatePrevious

GridlineColorIndex

SmallScroll

ActiveCell

Height

Split

ActiveChart

Index

SplitColumn

ActivePane

LargeScroll

SplitHorizontal

ActiveSheet

Left

SplitRow

Application

NewWindow

SplitVertical

Caption

OnWindow

TabRatio

Close

Panes

Top

Creator

Parent

Type

DisplayFormulas

PointsToScreenPixelsX

UsableHeight

DisplayGridlines

PointsToScreenPixelsY

UsableWidth

DisplayHeadings

PrintOut

View

DisplayHorizontalScrollBar

PrintPreview

Visible

DisplayOutline

RangeFromPoint

VisibleRange

DisplayRightToLeft

RangeSelection

Width

DisplayVerticalScrollBar

ScrollColumn

WindowNumber

DisplayWorkbookTabs

ScrollIntoView

WindowState

DisplayZeros

ScrollRow

Zoom



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:



192



Dim rng As Range

Dim rMin As Single

Set rng = Worksheets("Sheet1").Range("A1:D10")

rMin = Application.WorksheetFunction.Min(rng)



193



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

Figure 16-4. The Excel Application object and its children

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

×
x