Updated April 8, 2023
Excel VBA MsgBox (Table of Contents)
VBA MsgBox in Excel
VBA MsgBox is a popup dialog box on your Excel window showing a specific message. Most VBA programmers use it in their macro codes to make the code more interactive.
VBA Message Box (VBA MsgBox) is an inbuilt dialog box window that displays or shows a message alert box containing an optional set of buttons, icons, and other arguments settings.
Different parts of the VBA message box
- Message box Title: It is typically used to display what the VBA message box is about. It displays the application name “Microsoft Excel” by default if you don’t specify anything.
- Prompt or message text: The text message or string you want to convey through the message box to the user. i.e., the message that you want to display.
- Button(s): OK is the default button, where you can customize it to show two or more buttons such as Yes/No, Retry/Ignore, Yes/No/Cancel
- Close Icon: It is used to close the VBA message box by clicking on X symbol i.e Close icon.
Syntax for VBA MsgBox Function
MsgBox prompt, [buttons],[title],[helpfile],[context]
Or
=MSGBOX(Text_String , [buttons] ,[title], [helpfile, context])
Where,
- Prompt or Text_String – This is a required parameter or argument. It displays the message that you see in the MsgBox. In our example, the text “This is my first Programme” is the ‘prompt’. You must always enclose the text string in double-quotes; the limitation is 1,024 characters. This space can be used to write single or multiple-line text or display tables/data.
- [buttons] – It is an optional argument, which indicates the number and type of buttons which you want to display in the VBA MsgBox. E.g., suppose if you enter “0” as a number of integral value (Button parameter constant) or use vbOkOnly, it will show the OK button only in MsgBox.
- [title] – It is an optional argument; this is displayed at the top in the title bar of the VBA MsgBox. If you don’t enter any parameter or left blank in the title argument, then by default, it will show an application name, i.e. “Microsoft Excel.”
- [helpfile] – It is an optional argument; it’s a string expression which path to help. If you’re using a help file argument in VBA MsgBox syntax, then you need to enter context argument and vice versa.
- [context] – It is an optional argument; it is a numeric expression, i.e. Help context number assigned to the appropriate Help topic.
Note: The arguments mentioned in the square brackets are optional. Only the first parameter, i.e. ‘prompt’ argument is mandatory & you can ignore the [helpfile] and [context] arguments which are rarely used
Constants for VBA MsgBox
Buttons and icons are a combination of the Buttons argument, which is a unique value that drives the output to users to check out button combinations:
VBA MsgBox Styles
20 Button parameter constants
It is used based on the requirement & to make the VBA message box interactive
Constant | Value | Description |
vbOKOnly | 0 | To Display OK button only. |
vbOKCancel | 1 | To Display OK and Cancel buttons. |
vbAbortRetryIgnore | 2 | To Display Abort, Retry, and Ignore buttons. |
vbYesNoCancel | 3 | To Display Yes, No, and Cancel buttons. |
vbYesNo | 4 | To Display Yes and No buttons. |
vbRetryCancel | 5 | To Display Retry and Cancel buttons. |
vbCritical | 16 | To Display Critical Message icon. |
vbQuestion | 32 | To Display Warning Query icon. |
vbExclamation | 48 | To Display Warning Message icon. |
vbInformation | 64 | To Display Information Message icon. |
vbDefaultButton1 | 0 | The first button is the default. |
vbDefaultButton2 | 256 | The second button is the default. |
vbDefaultButton3 | 512 | The third button is the default. |
vbDefaultButton4 | 768 | The fourth button is the default. |
vbApplicationModal | 0 | In this scenario, the user must or should respond to the message box before continuing work in the current application. |
vbSystemModal | 4096 | In this Scenario, All applications are suspended until the user responds to the message box. |
vbMsgBoxHelpButton | 16384 | It adds a Help button to the message box. |
VbMsgBoxSetForeground | 65536 | It indicates or Specifies the message box window as the foreground window. |
vbMsgBoxRight | 524288 | It is based on the Alignment; Here, the text is right-aligned. |
vbMsgBoxRtlReading | 1048576 | It indicates or Specifies text should appear as right-to-left reading on Hebrew and Arabic systems. |
The following applies to the VBA MsgBox function when the user clicks a button returning a unique value.
Constant |
Value |
Description |
vbOK | 1 | OK |
vbCancel | 2 | Cancel |
vbAbort | 3 | Abort |
vbRetry | 4 | Retry |
vbIgnore | 5 | Ignore |
vbYes | 6 | Yes |
vbNo | 7 | No |
How to Create VBA MSGBox in Excel?
Let us now see how to create VBA MsgBox in Excel with the help of an example.
To Create a vbOKOnly (Default) VBA message box, follow the below steps
- Open a VBA Editor window
Select or click on Visual Basic in the Code group on the Developer tab, or you can directly click on Alt + F11 shortcut key.
Now, you can see the VBA Editor window, under the project window, in the VBA project, you can see the work file listed (i.e. Sheet1 (VBA MsgBox Example)
- Create a module
To create a blank module, right-click on Microsoft Excel objects, in that, click on Insert and under the menu section, select Module, where the blank module gets created.
- Creating a VBA message box
Now the blank module is created, it is also called a code window, where you need to type Sub myfirstprogram() as the first message without any quotes around it. Now, you can observe, Excel automatically adds the line End Sub below the first message line when you press Enter.
Now, all the codes which you enter must be between these two lines; now, you can start typing the MsgBox function or syntax.
Once you start typing msgbox and leave a space, automatically syntax help feature appears
Now, I enter only the Prompt argument, i.e. “this is my first programme” remaining syntax or argument; I set it as a default
Now, the code is ready,
Sub myfirstprogram()
MsgBox "this is my first programme"
End Sub
you can run the macro by clicking the Run Sub button (i.e. green “play” button) or by pressing F5.
The VBA message box popup window appears.
Similarly, you can create different types of Message Boxes available in Excel VBA, i.e. you can add a title, select a different button argument of your choice & multiple lines of text.
VBA message box macro code with title & button argument
Sub mysecondprogram()
MsgBox "this is my first programme", vbOKCancel, "WELCOME TO VBA"
End Sub
Run the code by pressing F5. The VBA message box with title and button popup window appears
Save Macro code
Save your workbook as an “Excel macro-enabled workbook”. Press Crl + S, then a popup appears; in that, select or click on the “No” button.
“Save as” window popup appears, now you can save this file as an Excel Macro-Enabled workbook.
Once again, if you open a file, you can click on the shortcut key, i.e. ALT +F8, “Macro” dialog box appears, where you can run a saved macro code of your choice.
Things to Remember About the VBA MsgBox in Excel
- VBA message box is extremely useful when you are debugging
- You have an option to insert VBA MsgBox anywhere in your code
- VBA MsgBox will help you out when you want to ask the user a query.
Recommended Articles
This has been a guide to Excel VBA MsgBox. Here we discuss how to create VBA MsgBox in Excel along with practical examples and a downloadable excel template. You can also go through our other suggested articles –