VBA Msgbox - A Complete Guide To The VBA Message Box: Paulkelly 0 Com Ments
VBA Msgbox - A Complete Guide To The VBA Message Box: Paulkelly 0 Com Ments
The VBA MsgBox function is used to display messages to the user in the form of a message box.
We can configure the message box to provide the user with a number of different buttons such as
Yes, No, Ok, Retry, Abort, Ignore and Cancel. The MsgBox function will then return the button
that was clicked.
Contents [hide]
1 Basic VBA MsgBox Examples
2 VBA MsgBox Parameters
o 2.1 VBA MsgBox Return Values
o 2.2 VBA MsgBox Yes No
3 VBA MsgBox Button Parameters
4 VBA MsgBox Examples
5 Related Posts
Sub BasicMessageBox()
End Sub
Sub MessagesYesNoWithResponse()
Else
End If
End Sub
Note: When we return a value from the message box we must use parenthesis around the
parameters or we will get the “Expected end of statement” error.
We can also use a variable to store the response from the MsgBox. We would normally do this if
we want to use the response more than once. For example, if there were three buttons:
Sub Msgbox_AbortRetryIgnore()
End If
End Sub
vbDefaultButton4 3 Default Set button 4 to be selected. Note that there will only be four buttons if the help button is
button included with vbAbortRetryIgnore or vbYesNoCancel.
vbApplicationModal 4 Modal Cannot access Excel while the button is displayed. Msgbox is only displayed when Excel
is the active application.
vbSystemModal 4 Modal Same as vbApplicationModal but the message box is displayed in front of all
applications.
vbMsgBoxSetForeground 5 Other Sets the message box windows to be the foreground window
Constant Group Type Description
vbMsgBoxRtlReading 5 Other Specifies text should appear as right-to-left reading on Hebrew and Arabic systems.
When we use MsgBox, we can combine items from each group by using the plus sign. For
example:
This displays the message box with the Ok and Cancel button, the critical message icon, with the
Ok button highlighted and the message box will display only when Excel is the active
application.
This displays the message box with the Yes and No button, the warning query icon, with the No
button highlighted and the message box will display in front of all applications.
Important: Each time we use the MsgBox function we can only select one of each:
1. button type
2. icon type
3. default button
4. modal type
In other words, we can only select one item from each of the first 4 groups.
The next section shows some more examples of using the message box.
' Yes/No buttons with Warning Query icon and Yes button selected
' Yes/No buttons with Warning Message icon and Yes button selected
' Yes/No button with Information Message icon and Cancel button selected
The following examples show the Abort/Retry/Ignore button plus the help button with different
buttons selected:
' Abort/Retry/Ignore button with the Help button displayed and Abort
selected
' Abort/Retry/Ignore button with the help button displayed and Retry
selected
resp = MsgBox("Error", vbAbortRetryIgnore + vbDefaultButton2 +
vbMsgBoxHelpButton)
' Abort/Retry/Ignore button with the Help button displayed and Ignore
selected
' Abort/Retry/Ignore button with the Help button displayed and Help
selected
The following examples show some button selections and the title parameter being set:
' Retry/Cancel button with query warning as the icon and "Error" as the
title
' Ok button with critical icon and "System error" as the title