Inserting messagebox (MsgBox) vba code

The MsgBox function is the simplest way to communicate and interact with the user and therefore is much used. A MsgBox is used to:

  1. Communicate information, a warning or an error message;
  2. Ask the user a question.
Note
In some cases it is better to create dedicated userforms, which give better support to more complex user interaction with a combination of choices and the use of list boxes and other controls. Implementing a userform however is quite a bit of work.

Using MsgBox in combination with Select Case

In many cases the msgbox, in addition to providing information, is used to ask the user to make a decision concerning the further flow of the program. For example, if a contact does not exist, does he want to create a new one. The appropriate way to handle this is in a Select Case block where the different cases reflect to possible returned values of the MsgBox function. These values are from the VbMsgBoxResult enumeration, but are determined by the value of the Buttons argument. In the example below the value vbYesNoCancel indicates the user has three possible choices.

msgbox vbYesNoCancel vbDefaultButton1
Select Case MsgBox(Prompt:="Do you want to create a new contact", _
                    Buttons:=vbYesNoCancel + vbQuestion + vbDefaultButton2, _
                    Title:="Manage contacts")
Case vbYes
    
Case vbNo
    
Case vbCancel

End Select

Use the Code VBA add-in to insert MsgBox code

select msgbox code

The Code VBA add-in makes it a breeze to insert MsgBox code including the appropriate Select Case block.

Click this Start insertmsgbox demo screencast to see the insertion of msgboxcode using code vba.insert msgbox with select case

MsgBox Buttons argument

The Buttons argument type is VbMsgBoxStyle. This is the result of a combination of choices you can make with regard to

In the above example: Buttons:=vbYesNoCancel + vbQuestion + vbDefaultButton2 meaning:

Note
The + is used to combine the values which define the Button argument.

Add icon to dialog to convey the purpose of the interaction

Adding an icon to dialog to convey the purpose of the interaction makes you program more use friendly and makes it look nicer. The following icons can be used by including the appropriate constant to the Button argument: vbQuestion� icon vbQuestion vbExclamation icon  vbExclamation vbCritical icon vbCritical vbInformation icon vbInformation

Preselecting the button to be clicked

You can preselect the default button (the one to have the focus) by changing the digit at the end of vbDefaultButton1 to the requered button. In the above case vbDefaultButton2 gives the No button the focus. This could be useful if a certain button is used most often so the user only has to press Enter.

Setting the title bar

Your Office program fills the title bar with its name, but you can make better use of it by including info on the process the user is currently executing. In the above example we added "Manage contacts" for the Title argument.