Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Msgbox - No Default Button

Tags:

excel

vba

I would like to know if it's possile to show a Msgbox in VBA (Excel 2007), without any button being set as default? I have a yes\no button and I don't want the user to hit return or the space bar and accidentally trigger a default button.

Anyone know if this is possible so that neither button on my form is set to default, forcing the user to click yes or no?

If MsgBox("Message goes here", vbYesNo + vbQuestion ) = vbYes Then
    'Something
Else
    'Else
End If
like image 689
Eddie Avatar asked Jan 11 '23 20:01

Eddie


2 Answers

Another workaround (other than a UserForm, or trapping Enter & Space etc) is to

  1. Add a third button option that is an invalid choice
  2. Make this button the default
  3. Continue to show the message box if this option is picked

code

Dim lngMsg As Long
Do
lngMsg = MsgBox("Message goes here (Yes or No)", vbQuestion + vbYesNoCancel + vbDefaultButton3)
Loop While lngMsg = vbCancel
If lngMsg = vbYes Then
' route a
Else
' route b
End If
like image 137
brettdj Avatar answered Jan 27 '23 11:01

brettdj


No this is not possible unfortunately.

A (not particularly nice) workaround would be to code the message box using a UserForm. Probably not worth doing given the benefit over the extra code you'd have to maintain.

like image 25
Bathsheba Avatar answered Jan 27 '23 12:01

Bathsheba