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.

0 0

Post a comment