1 , Role
Displays the information in a message box and waits for the user to click the button, returning the clicked button value (such as "OK" or "Cancel"). Typically used as a way to display variable values.
2 , grammar
MsgBox (Prompt[,buttons][,title][,helpfile,context])
Parameter description:
(1) Prompt, a required parameter, is a string that is displayed as the message text in a message box. The maximum length is approximately 1024 characters, which is determined by the width of the characters used. If the content of the prompt is more than one line, it is possible to separate lines by a carriage return (CHR (13)), a newline (Chr (10)), or a combination of carriage return and newline (Chr (10)) between each line.
(2) Buttons, optional parameters, which are the sum of the values of the numeric expression, specify the number and form of buttons to display, the icon style used, the default button, and the mandatory response of the message box, etc., which can be customized with this message box. If this argument is omitted, its default value is 0. The setting values are shown in the following table.
(3) Title, an optional parameter that represents the text displayed in the title bar of the message box. If this argument is omitted, the application name is placed in the title bar.
(4) Helpfile, optional parameter, is a string expression that provides a Help file. If there is a helpfile, then there must be a context.
(5) The Context, optional parameter, is a numeric expression that provides a Help topic. If there is a context, then there must be helpfile.
3 , Button Parameters
Constants for the button parameter in the MsgBox function
Symbolic constants |
Value |
Role |
Button type |
vbOKOnly |
0 |
Show the OK button only |
vbOKCancel |
1 |
Show "OK" and "Cancel" button |
Vbabortretryignore |
2 |
Show "Terminate", "Retry", and "Ignore" buttons |
vbYesNoCancel |
3 |
Show Yes, no, and Cancel buttons |
vbYesNo |
4 |
Show Yes, no button |
Vbretrycancel |
5 |
Show "Retry" and "Cancel" button |
|
Icon Style |
vbcritical |
16 |
Display the critical message icon (System fork icon) |
Vbquestion |
32 |
Display the warning query icon (System question mark icon) |
Vbexclamation |
48 |
Display the warning message icon (System exclamation icon) |
vbinformation |
64 |
Display the information message icon (System information icon) |
|
|
|
|
Default button |
Vbdefauitbutton1 |
0 |
The first button is a default value |
Vbdefauitbutton2 |
256 |
The second button is a default value |
Vbdefauitbutton3 |
512 |
The third button is a default value |
Vbdefauitbutton4 |
768 |
The fourth button is a default value |
|
|
|
|
Forced return |
Vbapplicationmode1 |
0 |
Application forced return |
Vbsystemmode1 |
4096 |
System forced return |
|
|
|
|
Other types |
vbMsgBoxHelpButton |
16384 |
Add a Help button to the dialog box |
vbMsgBoxSetForeground |
65535 |
Specify dialog window as foreground window |
vbMsgBoxRight |
524288 |
The text of the dialog box is right-justified |
Vbmsgboxrtireading |
1048576 |
Specifies that the elements in the dialog box are displayed from right to left |
Description
(1) The first set of values (0–5) describes the type and number of buttons displayed in the 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 the message box. When these numbers are added to generate the buttons parameter values, only one number can be taken from each set of values.
(2) These constants are specified by VBA and can be used in the program code in place of the actual values.
4 , return value
Constants for the return value of the MsgBox function
Constant |
Value |
Description |
vbOK |
1 |
Are you sure |
Vbcancel |
2 |
Cancel |
Vbabort |
3 |
Terminate |
Vbretry |
4 |
Retry |
Vbignore |
5 |
Ignore |
Vbyes |
6 |
Is |
Vbno |
7 |
Whether |
5 , related instructions
(1) If both the HelpFile and the context parameters are available, you can press the F1 key to view the appropriate Help topic for the context, and Excel will usually automatically add a Help button in the input box.
(2) If the Cancel button is displayed in a message box, pressing the ESC key is the same as clicking the Cancel button. If there is a Help button in the message box, provide the relevant help information.
(3) If you want to enter multiple parameters and omit some of the intermediate parameters, you must add a comma delimiter at the appropriate location.
6 , Example
(1) Use the MsgBox function to display a critical error message in a dialog box with Yes and no buttons. The default button in the example is no, and the return value of the MsgBox function depends on which button the user presses. And assume that Demo.hlp is a Help file, which has a Help topic code of 1000.
- Dim msg,style,title,help,ctxt,response,mystring
- ' Define message text
- Msg= "Does want to continue?"
- ' Define button
- Style = vbYesNo + vbcritical + vbDefaultButton2
- ' Define title text
- Title = "MsgBox demonstration"
- ' Define Help file
- Help = "DEMO. HLP "
- ' Define Help topics
- Ctxt = 1000
- Response = MsgBox (MSG, Style, Title, help, Ctxt)
- If Response = vbyes Then ' user presses ' yes '
- MyString = "Yes"
- Else ' user pressed ' no '
- MyString = "No"
- End If
(2) Show only a message
- MsgBox "Hello!"
(3) Assigning the result of a message box to a variable
- Ans=msgbox ("Continue?", vbYesNo)
- If MsgBox ("Continue?", vbYesNo) <>vbyes then Exit Sub
(4) Use a combination of constants, assign a group config variable, and set the second button as the default button
- Config=vbyesno+vbquestion+vbdefaultbutton2
(5) To force a line break in a message, use the vbCrLf (or vbNewLine) constant in the text, separated by A & space and a character. Such as
- MsgBox "The first line." & vbNewLine & "Second line."
(6) You can insert a tab character in a message box using the Vbtab constant. The following procedure uses a message box to display all values in a 5x5 range of cells, separates columns with Vbtab constants, and inserts a new row using the vbCrLf constant. Note that the MsgBox function displays up to 1024 characters, thus limiting the number of cells that can be displayed.
- OPTION&NBSP;EXPLICIT&NBSP;&NBSP;
- Sub showrangevalue ()
- &NBSP;&NBSP;DIM&NBSP;MSG&NBSP;AS&NBSP;STRING&NBSP;&NBSP;
- dim r as integer, c as integer
- &NBSP;&NBSP;MSG = "" &NBSP;&NBSP;
- for r = 1 to 5
- &NBSP;&NBSP;&NBSP;&NBSP;FOR&NBSP;C&NBSP;=&NBSP;1&NBSP;TO&NBSP;5&NBSP;&NBSP;
- msg = msg & cells (r, c) & vbTab
- &NBSP;&NBSP;&NBSP;&NBSP;NEXT&NBSP;C&NBSP;&NBSP;
- Msg = msg & vbcrlf
- &NBSP;&NBSP;NEXT&NBSP;R&NBSP;&NBSP;
- msgbox msg
- END&NBSP;SUB&NBSP;&NBSP;
(7) Use a worksheet function in a message box statement and set the display's number format, as shown in the following statement:
- MsgBox "Selection have" & M & "cells." & Chr & "The sum is:" & Application.worksheetfunction. Sum (Selection) & Chr & "The average is:" & Format (Application.WorksheetFunction.Average (Selection), "#, # #0. "), vbinformation," Selection Count & sum & Average "& Chr (13)
Source: http://blog.csdn.net/bright_chen7/article/details/6248715
Reference: https://www.baidu.com/link?url=lRwVSdVDBPcm5ww-CkxqlyM4ColOU1VodWiHGsA-LgQnmYahXESn7nydeZmAo9rz&wd= &eqid=c33f9e7100001a8b00000004584f93a7
Parameters and usage of MsgBox in VBA