VBA MsgBox

The MsgBox is a dialog box in the excel VBA that can be used to inform the users of your program.

It displays a pop-up style message box and waits for the user to click a button, and then an action is performed based on the clicked button by the user.

It provides a way for the end-users to interact with a workbook. It can be a simple alert to the users, or it can be complicated that needs an action to proceed by the users.

Syntax

Prompt: (required parameter) it refers to a text that is displayed as a message in the dialog box. The maximum length of the posted message can be 1024 approximately. If the word crosses the defined range, then the message will be divided using the carriage return character (Chr(13)) or a linefeed character (Chr(10)) between each line.

Buttons: (optional parameter) it denotes a numeric expression to display the types of buttons, to use the icon style, the identity of the default button, and the modality of the message box. The default value for buttons is zero if the left side of the button is blank.

Title: (optional parameter) the title bar of the dialog box displayed the string expression. If the left side of the dialog box is blank, then the application name is placed in the title bar.

Helpfile: A string parameter that identifies the help file to use for providing context-sensitive help for the dialog box.

Context: The help author assigns the Help context number to an appropriate topic. If the context is provided, then the help file must also be provided.

We can configure the message box in many ways with the help of the button parameters. That shows in the below table, such as:

ConstantValueDescription
vbOKOnly0Display OK button only
vbOKCancel1Display OK and Cancel buttons
vbAbortRetryIgnore2Display Abort, Retry and Ignore buttons
vbYesNoCancel3Display Yes, No, and Cancel buttons
vbYesNo4Display Yes and No buttons
vbRetryCancel5Display Retry and Cancel buttons
vbCritical16Display Critical Message icon
vbQuestion32Display Warning Query icon
vbExclamation48Display Warning Message icon
VbInformation64Display Information Message icon
vbDefaultButton10The first button is default
vbDefaultButton2256The second button is default
vbDefaultButton3512The third button is default
vbDefaultButton4768The fourth button is default
vbApplicationModal0The user must respond to the message box
vbSystemModal4096All applications are suspended until the user responds to the message box
vbMsgBoxHelpButton16384Adds Help button to the message box
vbMsgBoxSetForeground65536Specifies the message box window as the foreground window
vbMsgBoxRight524288Text is eight-aligned
vbMsgBoxRtlReading1048576Specifies text should appear as right-to-left reading on Arabic and Hebrew systems

The above values are divided into four groups as the first group of values (0-5) describes the number and type of buttons displayed in the dialog box. The second group of values (16, 32, 48, and 64) illustrates the icon style. The third group of values (0, 256, and 512) determines which button is the default. And the modality of the message box is defined in the fourth group (0, 4096). We can add only one number from each group to create a final value for the buttons argument.

Return Values

The MsgBox function returns any one value from the following values, which is used to identify the button. And the only thing that the user has to do that clicked in the message box.

ConstantValueDescription
vbOK1OK
vbCancel2Cancel
vbAbort3Abort
vbRetry4Retry
vbIgnore5Ignore
vbYes6Yes
vbNo7No

Examples

Suppose we want to display a message box with Yes, No, and Cancel buttons, as shown in the below code:

VBA MsgBox

Executes the above function by clicking on the run button on the VBA window. It displays a “Welcome” message box within a message box and an “OK” button.

VBA MsgBox

After clicking the OK button, another dialog box is displayed with a message “do you like the red color” and “yes”, “no”, and “cancel” buttons.

VBA MsgBox

After clicking any button (e.g., yes), the value of that button is stored as an integer. And it displayed a pop-up message box to the user, as shown below. Using this value, we can understand which button was clicked by the user.

VBA MsgBox


Next TopicVBA Comment