First, function syntax
Function Name: MsgBox
function function: Displays a message in a dialog box, waits for the user to click the button, and returns an Integer telling the user which button to click.
function Syntax:
MsgBox (prompt[, buttons] [, title] [, HelpFile, context])
Parameter description:
The syntax for the MsgBox function has several named arguments:
Prompt: Required. A string expression that is the message that appears in the dialog box.
The maximum length of the prompt is approximately 1024 characters, determined by the width of the characters used.
If the contents of a prompt exceed a row, the rows can be separated by a carriage return (CHR (13)), a newline character (CHR (10)), or a combination of a carriage return and a newline character (Chr & Chr (10)).
Buttons: Optional. A numeric expression is the sum of the values, specifying the number and form of the display buttons, the icon style used, what the default button is, and the mandatory response of the message box. If omitted, the default value for buttons is 0.
Title: Optional. A string expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar.
Helpfile: Optional. A string expression that identifies the Help file that is used to provide context-sensitive Help to the dialog box. If a helpfile is provided, the context must also be provided.
Context: Optional. A numeric expression that is assigned by the author of the Help file to the Help context number of the appropriate Help topic. If the context is provided, helpfile must also be provided.
Button parameter
Constant Value Description
vbOKOnly 0 Only displays the OK button
vbOKCancel 1 Displays the OK and Cancel buttons
Vbabortretryignore 2 Displays the Terminate, retry, and Ignore buttons
vbYesNoCancel 3 shows Yes, no and Cancel buttons
vbYesNo 4 shows Yes and no buttons
Vbretrycancel 5 Displays the Retry and Cancel buttons
vbcritical 16 Displays the "Key information" icon
Vbquestion 32 Displays the warning query icon
Vbexclamation 48 Displays the warning message icon
vbinformation 64 Displays the notification message icon
VbDefaultButton1 0 The first button is the default value (the default setting)
VbDefaultButton2 256 The second button is the default value
VbDefaultButton3 512 The third button is the default value
VbDefaultButton4 768 The fourth button is the default value
vbApplicationModal 0 Application force return; The application is suspended until the user has a message box
Respond before continuing to work
Vbsystemmodal 4096 system force return; All applications are suspended until the user responds to a message box to continue working
vbMsgBoxHelpButton 16384 Add the Help button to the message box
vbMsgBoxSetForeground 65536 Specifies the message box window as the foreground window
vbMsgBoxRight 524288 text for right alignment
vbMsgBoxRtlReading 1048576 Specifies that the text should be displayed from right to left in the Hebrew and Arabic systems
Description
① the first set of values (0–5) describes the type and number of buttons displayed in a message box, the second set of values (16,32,48,64) describes the style of the icon, and the third set of values (0,256,512,768) indicates which button is the default, and the fourth set of values (0,4096) Determines the mandatory return of a message box. When these numbers are added to generate buttons parameter values, only one number can be fetched from each set of values.
① These constants are specified by Visual Basic for Applications (VBA). As a result, you can use these constant names everywhere in your program code without having to use actual values. The actual value is equivalent to the constant name.
return value
Constants for MsgBox function return values
Constant numerical description
vbOK 1 OK
Vbcancel 2 Cancel
Vbabort 3 terminated.
Vbretry 4 Retry
Vbignore 5 Ignore
Vbyes 6 is
Vbno 7 No
① If you provide both the HelpFile and the context parameters, you can press the F1 key to see the appropriate Help topic for the context, and Excel usually automatically adds a Help button to the input box.
② If you display the Cancel button in a message box, pressing the ESC key will have the same effect as clicking the Cancel button. If the Help button is available in the message box, provide related help information.
③ If you want to enter multiple parameters and omit some of the middle parameters, you must add a comma delimiter at the appropriate location.