VBA basics
23. Message box (MsgBox function)

Excel macro basics and applications, introduction to Excel VBA
last updated:08-30-2019

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

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



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:

named argument.

An argument with a predefined name.
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.

* Named arguments will be explained in detail later.
25.About named arguments
An argument is a specification of a value passed when calling a function or method. There is a named argument as a description method when passing this argument. In the macro VBA description,: = is used.

However, in MsgBox, you don't often use named arguments, they are specified in order.

The?buttons?argument?settings are:

group Constant Value Description
Button type vbOKOnly 0 Display?OK?button only.
vbOKCancel 1 Display?OK?and?Cancel?buttons.
vbAbortRetryIgnore 2 Display?Abort,?Retry, and?Ignore?buttons.
vbYesNoCancel 3 Display?Yes,?No, and?Cancel?buttons.
vbYesNo 4 Display?Yes?and?No?buttons.
vbRetryCancel 5 Display?Retry?and?Cancel?buttons.
Icon type vbCritical 16 Display?Critical Message?icon.
vbQuestion 32 Display?Warning Query?icon.
vbExclamation 48 Display?Warning Message?icon.
vbInformation 64 Display?Information 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

The MsgBox function returns the following return value.

Constant Value Description
vbOK 1 OK
vbCancel 2 Cancel
vbAbort 3 Abort
vbRetry 4 Retry
vbIgnore 5 Ignore
vbYes 6 Yes
vbNo 7 No

Example of MsgBox function



MsgBox "it is complete."

VBA macro msgbox

When only a message is specified.
Only the “OK” button is displayed.

MsgBox "it is complete." & vbLf & "Was good"

VBA macro msgbox

ヘルプでは
When multiple lines are specified, carriage return (Chr (13)), line feed (Chr (10)), or a combination of carriage return and line feed (Chr (13) & Chr (10)) Insert and separate lines.
It has become, but

Carriage return (Chr (13)) is VbCr
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"

VBA macro msgbox

When a message, button type, and title are specified.
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
Case vbYes
MsgBox "Yes was pressed."
Case vbNo
MsgBox "No was pressed."
End Select

VBA macro msgbox

Set the button type to "Yes" or "No"
Icon type, inquiry icon
Standard button, second button
Check how the message box appears.

The pressed button is confirmed by the return value.
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

Macro VBA processing time is fast and automatic,
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



  • >
  • >
  • 23. Message box (MsgBox function)

  • If you find this site useful, please share and bookmark.


    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.




    If you find this site useful, please share and bookmark.
    To the bottom of the text