VBA MsgBox Syntax


Here is the Syntax of MsgBox in VBA. Understand the syntax to create message boxes in Excel VBA, Access VBA, PowerPoint VBA, Outlook VBA and Word VBA.

In this topic:

  1. Syntax
  2. Parameters
  3. Examples

Syntax of VBA MsgBox Function

Following is the syntax of the VBA MsgBox Function. Message Box function has one required parameter and four optional parameters.

MsgBox (prompt, [ buttons, ] [ title, ] [ helpfile, context ])


MsgBox Function have the following named arguments. Message Box function accepts the 5 Parameters. And the first one is Prompt, it is the required Parameter. And the 4 parameters are optional.

MsgBox Function will process the given arguments values and shows the pop up box. We can pass the prompt and title as strings or variable values. And we can also provide the button values from the predefined constants. We can pass the values to the MsgBox Function based on our VBA automation requirement.

  • Prompt: Prompt is the first parameter in MsgBox Syntax, it is a mandatory parameter. Takes the message as string which you wants to show in the message box body.  Prompt is the required parameter,we must pass an argument value to the message box prompt parameter.
  • Buttons: Button is the Second Parameter in Message Box Syntax, it is an Optional Arguments to set the buttons to show on Message Box Window. We can change the default buttons and style of the message box using the button parameters. VBA MsgBox Function has the predefined list of the constants to set the button styles. This is an optional parameter, the default value of the buttons is vbOKOnly. This will show the message box with simple OK button.
  • Title: Title is the third Parameter in the Syntax, it is an Optional Parameter. We can provide the title of the message box window using Title parameter. This is an optional parameter, the default title of the message box is the name of the Application.
  • Helpfile: helpfile parameter is useful to set the context-sensitive identifier. This is an optional parameter, context parameter is required when you provide helpfile parameter.
  • Context: to prove the context number set to the given help topic. This is an optional parameter, helpfile parameter is required when you provide context parameter.

VBA message box syntax examples

Here are the useful examples using VBA MsgBox Syntax. You can use these examples in MS Word, Excel VBA, Access VBA and Other MS Office Applications.

Example 1 : Displaying Hello World Message Box

Here is the simple Hello World Example using Message Box Function Syntax. We are providing the first argument as message box Prompt.

Sub Example1_HelloWorldMessageBox()

MsgBox "Hello World!"

End Sub

Example 2: Showing Simple Message Box using Named Arguments

The following Example Syntax displays the MsgBox using Named Arguments.

Sub Example2_MessageBoxWithNamedArguments()

MsgBox Prompt:="Hello World! using Named Argument 'Prompt'"

End Sub

Example 3: MsgBox Syntax to Display Icons

Here is the most useful example to display the message box with required icons.

Sub Example3_MessageBoxWithIcons()

MsgBox "Hi, This is the information icon", vbInformation

End Sub

Example 4: MsgBox Syntax for Ok and Cancel Buttons

The following Example displays Ok and Cancel buttons on VBA Message Box Window. You can change this to display any buttons on message box window.

Sub Example4_MessageBoxWithOkCancel()

MsgBox "Example on OK Cancel MsgBox in VBA", vbInformation + vbOKCancel

End Sub

Example 5: Syntax to return the Message Box user Click to a Variable

We can capture the user input of the Message Box. You can as user to click on a button based on the decision. And we can return the user clicked value to a variable for further use.

Sub Example5_MessageBoxRetrunValue()

varUserInput = MsgBox("Click one of the follwing buttons", vbYesNo)
If varUserInput = vbYes Then MsgBox "You clicked Yes button"
If varUserInput = vbNo Then MsgBox "You clicked No button"

End Sub


VBA MsgBox Function

Leave a Comment