MODULES▼
LESSONS▼
TOPICS▼
When a message box is displayed you can press Enter on the keyboard to click its default button. The default button will usually be the left-most one of the set:

Connoisseurs of 1980s movies will appreciate the importance of not accidentally choosing Yes when asked this question by a computer...
Files Needed
You don't need any files for this part of the lesson.
Completed Code
You can click here to download a workbook containing the sample code.
Setting a Default Button
You can set a different button as the default by adding a constant in the Buttons parameter of a message box. In a new workbook, insert a module and create a subroutine called DefaultButton. Add code to display a message box as shown below:

You can add a default button option to the Buttons parameter by adding it to the choice of buttons with the + symbol.
Including an Icon
You can combine a choice of default button with a choice of icon:

Specify your choice of buttons, default button and icon in the Buttons parameter.
Running the above code displays a message box which resembles the one shown below:

The only winning move is not to play.
To practise setting a default button on a message box:
- Using any workbook, add a new subroutine called IfAtFirst.
- Add code which displays a message box with a customised prompt, icon and buttons:
Sub IfAtFirst()
MsgBox _
"If at first you don't succeed...", _
vbQuestion + vbAbortRetryIgnore
End Sub
- Extend the code so that the message box defaults to the Retry button:
Sub IfAtFirst()
MsgBox _
"If at first you don't succeed...", _
vbQuestion + vbAbortRetryIgnore + vbDefaultButton2
End Sub
- Run the subroutine to check that your message appears as expected:

Feel free to set one of the other buttons as the default if you have a different attitude to failure.
- Save and close the workbook.