DefaultButton Property

Applies To

Button Object, Buttons Collection, DialogSheet Object, DrawingObjects Collection.

Description

For Button objects, this property is True if the button is the default button for a user-defined dialog box. When the user presses the ENTER key, the default button is selected, and Microsoft Excel runs the macro identified by the button's OnAction property. Applies only to buttons on a dialog sheet. Read-write.

For DialogSheet objects, this property is the name of the button that is the default button while a dialog box is running. Read-write.

Remarks

When the dialog box is not displayed, this property can be used with the Button object to return or set the initial default button (the default button when the dialog is first displayed). Only one button on the dialog box can be the default button. Setting this property resets the property for all other buttons on the dialog sheet.

While the dialog box is running, use this property in an event procedure to return or set the current default button. In this case, the property is the button ID as a string, and the property is applied to the DialogSheet object. You cannot apply this property to a Button object while the dialog box is running.

See Also

CancelButton Property.

Example

This example sets button four to be the initial default button for Dialog1. You can only set the initial default button when the dialog box is not running.


DialogSheets("Dialog1").Buttons(4).DefaultButton = True

This example sets button five to be the current default button on Dialog1. You can only set the current default button while the dialog box is running (which means that this example must be in an event procedure for another control on Dialog1).


With DialogSheets("Dialog1")
    .DefaultButton = .Buttons(5).Name
End With