Message Box in Excel VBA | Part 4
ฝัง
- เผยแพร่เมื่อ 4 ม.ค. 2025
- Message Box in Excel VBA:
Requirement: Microsoft Excel 2019
The VBA MsgBox function is used to display messages to the user in the form of a message box.
Syntax:
MsgBox( prompt [, buttons ] [, title ] [, helpfile, context ] )
prompt - This is a required argument. It displays the message that you see in the MsgBox.
In our example, the text “Welcome to VBA Message Box” is the ‘prompt’. You can use up to 1024 characters in the prompt, and can also use it to display the values of variables.
In case you want to show a prompt that has multiple lines, you can do that as well .
[buttons] - It determines what buttons and icons are displayed in the MsgBox. For example, if I use vbOkOnly,
it will show only the OK button, and if I use vbOKCancel, it will show both the OK and Cancel buttons. I will cover different kinds of buttons later in this tutorial.
[title] - Here you can specify what caption you want in the message dialog box. This is displayed in the title bar of the MsgBox.
If you don’t specify anything, it will show the name of the application.
[helpfile] - You can specify a help file that can be accessed when a user clicks on the Help button. The help button would appear only when you use the button code for it. If you’re using a help file, you also need to also specify the context argument.
[context] - It is a numeric expression that is the Help context number assigned to the appropriate Help topic.
Example:
Some Common Button Options
vbOKOnly: Displays OK button only.
vbOKCancel: Displays OK and Cancel buttons.
vbYesNo: Displays Yes and No buttons.
vbYesNoCancel: Displays Yes, No, and Cancel buttons.
Example with User Response:
You can capture user responses like this:
VBA MsgBox Button Constants:
The button parameter of MsgBox allows us to configure the message box in many ways.
The table below shows the different options:
Constant Group Type Description
vbOKOnly 1 Buttons Ok button.
vbOKCancel 1 Buttons Ok and cancel buttons.
vbAbortRetryIgnore 1 Buttons Abort, Retry and Ignore buttons.
vbYesNoCancel 1 Buttons Yes, No and Cancel buttons.
vbYesNo 1 Buttons Yes and No buttons.
vbRetryCancel 1 Buttons Retry and Cancel buttons.
vbCritical 2 Icon Critical Message icon.
vbQuestion 2 Icon Question mark icon.
vbExclamation 2 Icon Warning Message icon.
vbInformation 2 Icon Information Message icon.
vbDefaultButton1 3 Default button Set button 1 to be selected.
vbDefaultButton2 3 Default button Set button 2 to be selected.
vbDefaultButton3 3 Default button Set button 3 to be selected.
vbDefaultButton4 3 Default button Set button 4 to be selected. Note that there will only be four buttons if the help button is included with vbAbortRetryIgnore or vbYesNoCancel.
vbApplicationModal 4 Modal Cannot access Excel while the button is displayed. Msgbox is only displayed when Excel is the active application.
vbSystemModal 4 Modal Same as vbApplicationModal but the message box is displayed in front of all applications.
vbMsgBoxHelpButton 5 Other Adds a help button
vbMsgBoxSetForeground 5 Other Sets the message box windows to be the foreground window
vbMsgBoxRight 5 Other Right aligns the text.
vbMsgBoxRtlReading 5 Other Specifies text should appear as right-to-left reading on Hebrew and Arabic systems.
These constants work as follows:
The constants in group 1 are used to select the buttons.
The constants in group 2 are used to select icons.
The constants in group 3 are used to select which button is highlighted when the message box appears.
The constants in group 4 are used to set the modal type of the message box.
The constants in group 5 are used for various settings.