23. Message box (MsgBox function)
Some of this topic was processed by machine translation. Original
Displays a dialog box (small screen) that displays a message.
Users can select (“Yes”, “No”, “OK”, “CANCEL”, etc.) and receive the selection results.
To display a message box with macro VBA, use the MsgBox function.
MsgBox function syntax
|prompt||Required.?String expression?displayed as the message in the dialog box. The maximum length of?prompt?is approximately 1024 characters, depending on the width of the characters used. If?prompt?consists of more than one line, you can separate the lines by using a carriage return character (Chr(13)), a linefeed character (Chr(10)), or carriage return - linefeed character combination (Chr(13) &?Chr(10)) between each line.|
|buttons||Optional.?Numeric expression?that is the sum of values specifying the number and type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box. If omitted, the default value for?buttons?is 0.|
|title||Optional. 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. String expression that identifies the Help file to use to provide context-sensitive Help for the dialog box. If?helpfile?is provided,?context?must also be provided.|
|context||Optional. Numeric expression that is the Help context number assigned to the appropriate Help topic by the Help author. If?context?is provided,?helpfile?must also be provided.|
The MsgBox function syntax has these named arguments:
With named arguments, you can set the values in any order without having to specify the values for each argument in the order specified in the syntax.
25.About named arguments
|Button type||vbOKOnly||0||Display?OK?button only.|
|Icon type||vbCritical||16||Display?Critical Message?icon.|
|Standard button||vbDefaultButton1||0||First button is default.|
|vbDefaultButton2||256||Second button is default.|
|vbDefaultButton3||512||Third button is default.|
|vbDefaultButton4||768||Fourth button is default.|
|Other||vbApplicationModal||0||Application modal; the user must respond to the message box before continuing work in the current application.|
|vbSystemModal||4096||System modal; all applications are suspended until the user responds to the message box.|
|vbMsgBoxHelpButton||16384||Adds?Help?button to the message box.|
|vbMsgBoxSetForeground||65536||Specifies the message box window as the foreground window.|
|vbMsgBoxRight||524288||Text is right-aligned.|
|vbMsgBoxRtlReading||1048576||Specifies text should appear as right-to-left reading on Hebrew and Arabic systems.|
"Other" would not need to be used.
Specify the total value by selecting one value from each group and adding them.
Consider combinations of message content, button types, and icon types.
MsgBox function return value constant
Example of MsgBox function
MsgBox "it is complete."
Only the “OK” button is displayed.
MsgBox "it is complete." & vbLf & "Was good"
Line feed (Chr (10)) is VbLf
The combination of carriage return and line feed (Chr (13) & Chr (10)) is VbCrLf.
Whichever you use, this is the same, but VbLf is fine.
MsgBox "I'm done", vbOKOnly, "Title"
Please check the title.
Dim rtn As Integer
rtn = MsgBox("Did you do it?", vbYesNo + vbQuestion + vbDefaultButton2, "Confirmation")
Select Case rtn 'Confirmation of pressed button
MsgBox "Yes was pressed."
MsgBox "No was pressed."
Icon type, inquiry icon
Standard button, second button
Check how the message box appears.
To get the return value,
Variable = MsgBox (...)
If it is, the argument is enclosed in parentheses.
How to use parentheses () in VBA
How to use MsgBox
There are many cases where you don't know when it's over.
In such a case, it is a good idea to tell MsgBox "Complete" at the very end of the macro VBA.
This is a message sent from the macro VBA to the user.
However, please note that users may feel annoyed if MsgBox is used unnecessarily.
Articles with the same theme "VBA basics"
20. Conditional branch (If)
21. Conditional branch (ElseIf)
22. Conditional branch (Select Case)
23. Message box (MsgBox function)
24. Input box (InputBox function)
25. About named arguments
26. General practice question 2
27. Select book / sheet (Select, Activate)
28. Select cell / row / column (Select, Activate)
29. Delete / Insert cell / row / column (Delete, Insert)
30. General practice question 3
I'm going to pay close attention to the description,
If you have any mistakes or suggestions,「Contact Us」I hope you will let me know.
Use the posted VBA code at your own risk.
We are not liable for any damages such as data corruption.