In Msgbox with YesNo or OkCancel buttons, the first button will be set as default button. i.e., once the message box appears if user hit Enter button, Yes or Ok will be activated.
Sometimes, this is not desired. In case user hits Enter key accidentally, it will result in executing code that user was not willing.
To avoid this we can set the 2nd or 3rd button, whichever is applicable as default button.
VBA Set 2nd Msgbox Button as Default:
Here is the sample code to make it happen.
Sub MsgboxTest() 'Set No as default x = MsgBox("2nd Button Default", vbYesNo + vbDefaultButton2) 'Set Cancel as default If x = vbNo Then MsgBox "3rd Button Default", vbYesNoCancel + vbDefaultButton3 End If End Sub
This by using vbDefaultButton 1,2,3 & so on, we can set the desired button as default.
This way, even if user type Enter key accidentally, the main operations wont be triggered. User gets a second chance to correct the action.
If this option is not available, then the programmers have to code a 2nd Msgbox asking double confirmation for very critical operations.
External Reference: Here is a discussion from stackoverflow discussing the same topic.