VBA MsgBox Function

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

VBA MsgBox Syntax

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.noConstant NameValue Description of Button
1.vbOKOnly0
Pop up OK button only.
2.vbOKCancel1Pop up OK & Cancel buttons.
3.vbAbortRetryIgnore2Pop up AbortRetry, & Ignore buttons.
4.vbYesNoCancel3Pop up YesNo, & Cancel buttons.
5.vbYesNo4Pop up Yes &No buttons.
6.vbRetryCancel5Pop up Retry & Cancel buttons.
7.vbCritical16
The critical Message icon will be Visible.
8.vbQuestion32
The warning Query icon will be Visible.
9.vbExclamation48
The warning Message icon will be Visible.
10.vbInformation64Information Message icon will be Visible.
11.vbDefaultButton10
1st button will be selected by default.
12.vbDefaultButton2256
2nd button will be selected by default.
13.vbDefaultButton3512
3rd button will be selected by default.
14.vbDefaultButton4768
4th button will be selected by default.
15.vbApplicationModal0when this button is used user have to respond first to this message box before doing any work in the current application.
16.vbSystemModal4096it 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.vbMsgBoxHelpButton16384
it is used to display a help button in the message box.
18.vbMsgBoxSetForeground65536
it will specify the message box window in the foreground window.
19.vbMsgBoxRight52488Text will appear in Right side
20.vbMsgBoxRtlReading1048576The message will appear from right to left.
MsgBox Button Parameter List

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.

Vba-Msgbox-Simple-Message

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.

Vba-Msgbox-Simple-Message-with-title
Title Displayed in MsgBox

#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

Vba-Msgbox-vbOKCancel
vbOKCancel

#3. vbAbortRetryIgnore: It will show Abort, Retry & Ignore Button in Message Box.

Sub SimpleMessage()

MsgBox “This is a simple message”, vbAbortRetryIgnore, “Information”

End Sub

Vba-Msgbox-vbAbortRetryIgnore
vbAbortRetryIgnore

#4. vbYesNoCancel: It will show Yes, No & Cancel Button in Message Box.

Sub SimpleMessage()

MsgBox “This is a simple message”, vbYesNoCancel, “Information”

End Sub

Vba-Msgbox-vbYesNoCancel

#5. vbCritical: This will show the critical icon in the message Box.

Sub SimpleMessage()

MsgBox “This is a simple message”, vbCritical, “Information”

End Sub

Vba-Msgbox-vbCritical

#6. vbQuestion: It is used to display the question mark icon.

Sub SimpleMessage()

MsgBox “This is a simple message”, vbQuestion, “Information”

End Sub

Vba-Msgbox-vbQuestion

#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

Vba-Msgbox-vbExclamation

#8. vbInformation: It will used to display the information icon in message Box.

Sub SimpleMessage()

MsgBox “This is a simple message”, vbInformation

End Sub

Vba-Msgbox-vbInformation

#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

vbYesNoCancel + vbDefaultButton1

#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

Vba-Msgbox-vbYesNoCancel + vbDefaultButton3 + vbMsgBoxHelpButton

#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

vbMsgBoxHelpButton

#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

vbYesNoCancel + vbDefaultButton3 + vbMsgBoxHelpButton + vbMsgBoxRtlReading

#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

vbYesNoCancel + vbDefaultButton3 + vbMsgBoxHelpButton + vbMsgBoxRight

 

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

vbnewline-Parameter

Next Chapter: Input_Box

Leave a Comment