Microsoft Office Tutorials and References
In Depth Information
9.5.6 Default Members
Set rng2 = rng
then the message box correctly displays True .
9.5.6 Default Members
In most object models, many objects have a default member (property or method) that is invoked
when a property or method is expected but we do not specify one. For instance, in the Microsoft
Word object model, the default member for the Range object is the Text property. Hence, the
VBA Word code:
Dim rng As Range
Set rng = ActiveDocument.Words(1)
rng = "Donna"
sets the first word in the active document to Donna, since Word applies the default property in the
last line, effectively replacing it with:
rng.Text = "Donna"
Unfortunately, neither the Excel VBA documentation nor the Excel object model make an effort
to identify the default members of Excel objects. Accordingly, my suggestion is to avoid the issue
when programming Excel.
In any case, default members tend to make code less readable, and for this reason, I generally
avoid them. One notable exception is for a collection object. It is generally the case that the
default member of a collection object is the Item method. Hence, for instance, we can refer to the
fourth cell in the current selection by:
Selection.Cells(4)
rather than by the more clumsy:
Selection.Cells.Item(4)
Since this use of the default member is not likely to cause any confusion, we will use it.
9.5.7 Global Members
Many of the properties and methods of the Application object can be used without qualifying them
with the word Application . These are called global members . For instance, the Selection
property is global, and so we can write:
Selection.Cells.Count
instead of:
Application.Selection.Cells.Count
To identify the global members, the Excel object model has a special object called the Global
object. This object is not used directly—its purpose is simply to identify the global members of
the object model. Note that the members of the Global object form a proper subset of the members
of the Application object (which means that not all of the members of the Application object are
global).
Search JabSto ::




Custom Search