VBA MsgBox function is a dialog box which is used to popup a message to the user.
VBA MsgBox Syntax

Description of MsgBox Syntax
Prompt − This is a Required Parameter. it will accept the input in String Format and the maximum length of this parameter is up to 1024 characters.
Buttons − This is an Optional Parameter and it’s a Numeric expression that allows the user to display predefined buttons, icons, icons styles. If the Button parameters are left blank, the default value for the button will be 0.
Title − This is an Optional Parameter and It’s a string expression that allows user to display the title bar in the dialog box. If the title is not specified, by default it will show Microsoft Excel which is the application name in the title bar.
Helpfile − This is also An Optional Parameter. For this parameter, we have to specify a help file that will be accessed whenever a user will click on the Help button. Context Parameter is mandatory to specify when we are using this parameter.
Context − This is an Optional Parameter. it’s a Numeric expression that is used very rarely. Whenever context parameter is used, then helpfile parameter must be used along with Context.
Note: All arguments is optional except Prompt argument.
Most Commonly Button Arguments list which are used while using the MsgBox Function.
S.no | Constant Name | Value | Description of Button |
1. | vbOKOnly | 0 | Pop up OK button only. |
2. | vbOKCancel | 1 | Pop up OK & Cancel buttons. |
3. | vbAbortRetryIgnore | 2 | Pop up Abort, Retry, & Ignore buttons. |
4. | vbYesNoCancel | 3 | Pop up Yes, No, & Cancel buttons. |
5. | vbYesNo | 4 | Pop up Yes &No buttons. |
6. | vbRetryCancel | 5 | Pop up Retry & Cancel buttons. |
7. | vbCritical | 16 | The critical Message icon will be Visible. |
8. | vbQuestion | 32 | The warning Query icon will be Visible. |
9. | vbExclamation | 48 | The warning Message icon will be Visible. |
10. | vbInformation | 64 | Information Message icon will be Visible. |
11. | vbDefaultButton1 | 0 | 1st button will be selected by default. |
12. | vbDefaultButton2 | 256 | 2nd button will be selected by default. |
13. | vbDefaultButton3 | 512 | 3rd button will be selected by default. |
14. | vbDefaultButton4 | 768 | 4th button will be selected by default. |
15. | vbApplicationModal | 0 | when this button is used user have to respond first to this message box before doing any work in the current application. |
16. | vbSystemModal | 4096 | it will reflect the message box on the computer screen above all apps the user has to click on the message box then only it will get disappear |
17. | vbMsgBoxHelpButton | 16384 | it is used to display a help button in the message box. |
18. | vbMsgBoxSetForeground | 65536 | it will specify the message box window in the foreground window. |
19. | vbMsgBoxRight | 52488 | Text will appear in Right side |
20. | vbMsgBoxRtlReading | 1048576 | The message will appear from right to left. |
VBA MsgBox Buttons Function
- The First Group of Constant which value is from (0-5) used to display the various types of Buttons.
- The Second Group of Constant which value is from (16-64) used to display the Various Icons in the Message Box.
- The Second Group of Constant (0,256,512,768) used to select the particulars buttons in MsgBox.
- The fourth Group of Constant (0,4096) used to display the message Box over a single application & all applications.
- Fifth Group of Constant used for various settings in VBA MsgBox Function.
Basic VBA MsgBox Example
#1. vbOKOnly : It is Default button parameters either we can use or without using we will get the OK button in the Message dialog box.
Sub SimpleMessage()
MsgBox “This is a simple message”
End Sub
This will show a Simple Message Box which has like Below.

So now we can see that in the MsgBox Title Microsoft Excel is reflecting so if we want to change this we will write the code as below.
Sub SimpleMessage()
MsgBox “This is a simple message”, , “Information”
End Sub
This will show same Message Box with Information title.

#2. vbOKCancel: This Parameters will show Ok and Cancel Button in Message Box.
Sub SimpleMessage()
MsgBox “This is a simple message”, vbOKCancel, “Information”
End Sub

#3. vbAbortRetryIgnore: It will show Abort, Retry & Ignore Button in Message Box.
Sub SimpleMessage()
MsgBox “This is a simple message”, vbAbortRetryIgnore, “Information”
End Sub

#4. vbYesNoCancel: It will show Yes, No & Cancel Button in Message Box.
Sub SimpleMessage()
MsgBox “This is a simple message”, vbYesNoCancel, “Information”
End Sub

#5. vbCritical: This will show the critical icon in the message Box.
Sub SimpleMessage()
MsgBox “This is a simple message”, vbCritical, “Information”
End Sub

#6. vbQuestion: It is used to display the question mark icon.
Sub SimpleMessage()
MsgBox “This is a simple message”, vbQuestion, “Information”
End Sub

#7. vbExclamation: It is used to display the Exclamatory icon in the dialog Box.
Sub SimpleMessage()
MsgBox “This is a simple message”, vbExclamation, “Information”
End Sub

#8. vbInformation: It will used to display the information icon in message Box.
Sub SimpleMessage()
MsgBox “This is a simple message”, vbInformation
End Sub

#9. vbDefaultButton1: This will select the first Button by default in the message Box.
Sub SimpleMessage()
MsgBox “This is a simple message”, vbYesNoCancel + vbDefaultButton1
End Sub

#10. vbDefaultButton3: It will select the third Button by default in the message Box.
Sub SimpleMessage()
MsgBox “This is a simple message”, vbYesNoCancel + vbDefaultButton3 + vbMsgBoxHelpButton
End Sub

#11. vbMsgBoxHelpButton: This will add the help button next to OK Button in the message Box.
Sub SimpleMessage()
MsgBox “This is a simple message”, vbMsgBoxHelpButton
End Sub

#12. vbMsgBoxRtlReading: In this message will display from Right to left Direction.
Sub SimpleMessage()
MsgBox “This is a simple message”, vbYesNoCancel + vbDefaultButton3 + vbMsgBoxHelpButton + vbMsgBoxRtlReading
End Sub

#13. vbMsgBoxRight: It will display the message text in Right instead of Left.
Sub SimpleMessage()
MsgBox “This is a simple message”, vbYesNoCancel + vbDefaultButton3 + vbMsgBoxHelpButton + vbMsgBoxRight
End Sub

How to Display a Message in new line By using VBA Message Box Function
So For using the new line under a Message box we have to use vbNewLine argument.
Sub SimpleMessage()
MsgBox “Line1-Hello” & vbNewLine & “Line 2-World”
End Sub
