This is an overview:
Excel VBA MsgBox
Syntax of the VBA MsgBox Function in Excel
MsgBox( prompt [, buttons ] [, title ] [, helpfile, context ] )
- prompt – shows the message.
- [buttons] – The buttons to show: Yes/No, Yes/No/Cancel, Retry/Ignore, etc.
- [title] – What the message box is about. If there is no title, it will show Microsoft Excel.
- [helpfile] – A helpfile is accessed when you use the Help button.
- [context] – The Help context number is a numeric value assigned to a specific Help topic that can be used to identify and access the topic within the Help system.
Note: The MsgBox function can work with one argument: prompt
How to Launch the VBA Editor in Excel
If the Developer tab is not available on the ribbon, you need to enable it.
- Go to the Developer tab and select Visual Basic.
- Select Insert,
- Choose Module.
Example 1 – General Message Box with Title
Create a MsgBox with the title: “It is the Title”.
- In the VBA module, use the following code.
Sub MsgBox_Title()
MsgBox "This is a sample box", , "It is the Title"
End Sub
- Click Run or press F5.
MsgBox has a Title argument that is changed to “It is the Title”.
In the MsgBox prompt [, buttons ] [, title ] [, helpfile, context ] function, the title is provided as “It is the title”.
Read More: How to Use MsgBox and InputBox in VBA Excel
Example 2 – Creating a Yes/No MsgBox with the Title Confirmation Window
- In the VBA module, use the following code.
Sub Msgbox_continue()
result = MsgBox("Would you like to continue?", vbYesNo, "Confirmation Window")
End Sub
- Click Run or press F5.
This code creates a Yes/No MsgBox with the title “Confirmation Window”.
Code Breakdown
result = MsgBox("Would you like to continue?", vbYesNo, "Confirmation Window")
vbYesNo (the second argument, after the prompt) creates Yes and No buttons and the title: “Confirmation Window”.
Example 3 – Creating a Yes/No Warning MsgBox with Title
- In the VBA module, use the following code.
Sub Msgbox_continue()
result = MsgBox("Would you like to continue?", vbYesNo + vbCritical, "Warning Message")
End Sub
- Click Run or press F5.
The code creates a Warning MsgBox with title Warning Message.
Code Breakdown
result = MsgBox("Would you like to continue?", vbYesNo + vbCritical, "Warning Message")
vbYesNo+vbCritical creates Yes and No buttons and vbCritical creates the message box with the title: “Warning Message”.
Read More: Excel VBA: Show Multiple Values with Multiple Variables in MsgBox
Example 4 – Creating a Warning MsgBox with the Title “Failure” and Different Buttons
- In the VBA module, use the following code.
Sub Msgbox_continue()
result = MsgBox("Failed to Perform", vbAbortRetryIgnore + vbCritical, "Failure!")
End Sub
- Click Run or press F5.
Code Breakdown
result = MsgBox("Failed to Perform", vbAbortRetryIgnore + vbCritical, "Failure!")
- vbAbortRetryIgnore creates the Abort , Retry and Ignore buttons.
- vbCritical creates a warning MsgBox.
- Failure! is the title.
Example 5 – Creating Multiple MsgBoxes with Different Titles
- In the VBA module, use the following code.
Sub Msg_Response()
Dim user_response As Integer
Dim message_prompt As String
Dim message_title As String
message_prompt = "Failed to Perform"
message_title = "Failure!"
user_response = MsgBox(message_prompt, vbAbortRetryIgnore + vbCritical, message_title)
Select Case user_response
Case 3
MsgBox "Action Aborted", vbInformation, "Abort Window"
Case 4
MsgBox "Retry Failed", vbInformation, "Retry Window"
Case 5
MsgBox "Error Ignored", vbInformation, "Ignore Window"
End Select
End Sub
Code Breakdown
Dim user_response As Integer Dim message_prompt As String Dim message_title As String
A necessary variable is declared.
message_prompt = "Failed to Perform"
message_title = "Failure!"
The MsgBox title is assigned in the message_title variable.
user_response = MsgBox(message_prompt, vbAbortRetryIgnore + vbCritical, message_title)
Another variable is used to store the MsgBox command and the title.
Case 3
MsgBox "Action Aborted", vbInformation, "Abort Window"
“Case 3” If Abort is selected, the information MsgBox is displayed with the title Abort Window and with the prompt Action Aborted.
Case 4
MsgBox "Retry Failed", vbInformation, "Retry Window"
“Case 4” is the value for the selection of Retry. If Retry is selected, the information MsgBox is displayed with the title Retry Window.
Case 5
MsgBox "Error Ignored", vbInformation, "Ignore Window"
“Case 5” is the value for the selection of Ignore. If Ignore is selected, the information MsgBox is displayed with the title Ignore Window.
- Click Run or press F5.
You will see the first box.
- Click Abort to see the Information MsgBox below with the title Abort Window.
- If you select Retry, you will get another Information MsgBox named Retry Window.
- If you select Ignore, you will see another Information MsgBox named Ignore Window.
Different Types of MsgBox Icons in Excel VBA
1. vbExclamation MsgBox in VBA
The VBA code will create an Exclamation Box with the title “ExclamationTitle”.
- Create a module and enter the code below.
Sub CustomMsgBoxwithExcaimation()
MsgBox "It is an Exclamation!", vbExclamation, "ExclamationTitle"
End Sub
- Click Run or press F5.
Code Breakdown
MsgBox "It is an Exclamation!", vbExclamation, "ExclamationTitle"
vbExclamation creates an Exclamation box.
2. vbQuestion MsgBox in VBA
- Create a module and enter the code below.
Sub CustomMsgBoxwithQuestion()
MsgBox "It is a Question?", vbQuestion, "Question Title"
End Sub
- Click Run or press F5.
Code Breakdown
MsgBox "It is a Question?", vbQuestion, "Question Title"
vbQuestion creates a Question Message box in Excel.
How to Create VBA MsgBox with Different Types of Buttons
1. Message Box with OK and Cancel Button
- Create a module and enter the code below.
Sub CustomMsgBoxwithOKCancel()
MsgBox "Ok and Cancel is added", vbOKCancel, "OKCancel"
End Sub
- Click Run or press F5.
Code Breakdown
MsgBox "Ok and Cancel is added", vbOKCancel, "OKCancel"
vbOKCancel creates an OK and a Cancel Button.
2. Message Box with Yes, No, and Cancel Button
- Create a module and enter the code below.
Sub CustomMsgBoxwithYesNoCancel()
MsgBox "Yes,No,Cancel is added", vbYesNoCancel, "YesNoCancel"
End Sub
- Click Run or press F5.
Code Breakdown
MsgBox "Yes,No,Cancel is added", vbYesNoCancel, "YesNoCancel"
vbYesNoCancel creates the Yes, No, and Cancel buttons.
Read More: Create VBA MsgBox Custom Buttons in Excel
List of Available Button Types for VBA MsgBoxes in Excel
There are other buttons available to use within VBA as well. Here is a list of the button types you can use either as one or in combination with each other in Excel.
BUTTON CODES | VALUES | DESCRIPTION |
---|---|---|
vbOKOnly | 0 | Shows the OK button only (Default). |
vbOKCancel | 1 | Shows the OK and Cancel buttons. |
vbAbortRetryIgnore | 2 | Shows the Abort, Retry and Ignore buttons. |
vbYesNo | 3 | Shows the Yes and No buttons. |
vbYesNoCancel | 4 | Shows the Yes, No and Cancel buttons. |
vbRetryCancel | 5 | Shows the Retry and Cancel buttons. |
vbMsgBoxHelpButton | 16384 | Shows the Help Button. |
vbDefaultButton1 | 0 | Defines the first button default. |
vbDefaultButton2 | 256 | Defines the second button default. |
vbDefaultButton3 | 512 | Defines the third button default. |
vbDefaultButton4 | 768 | Defines the fourth button default. |
vbMsgBoxRight | 524288 | Right aligns the text. |
vbMsgBoxRtlReading | 1048576 | Sets text reading from the right to the left like in Arabic and Hebrew languages. |
Frequently Asked Questions (FAQ)
1. Can I use a variable in the title of a VBA MsgBox?
Answer: Yes, you can use a variable by concatenating the variable with the rest of the message using the “&” operator:
title = “Custom Title”
MsgBox “Hello world!”, vbOKOnly, title
2. How do I keep the user’s response to a VBA MsgBox?
Answer: The MsgBox function returns a value that corresponds to the button that the user clicked. Keep this value using a variable:
response = MsgBox(“Do you want to continue?”, vbYesNo)
3. What values can be returned by a VBA MsgBox?
Answer: The MsgBox function can return the following values: vbOK, vbCancel, vbAbort, vbRetry, vbIgnore, vbYes, vbNo. (depending on the clicked button).
4. How can I customize the buttons that are displayed in a VBA MsgBox?
Answer: Use the “Buttons” argument to specify the buttons to be displayed. For example MsgBox “Do you want to continue?”, vbYesNoCancel
5. How can I customize the icon that is displayed in a VBA MsgBox?
Answer: Use the “Icon” argument to specify the icon to be displayed. For example MsgBox “Error!”, vbCritical
6. Can I use line breaks in the message displayed by a VBA MsgBox?
Answer: Yes, use the vbCrLf constant to insert line breaks in the message displayed by a MsgBox. For example MsgBox “First line” & vbCrLf & “Second line”
Download Practice Workbook
Download the Excel workbook.