MsgBox Parameters
Here are the parameters for the MsgBox function.
MsgBox(prompt,buttons,title,helpfile,context)
Only prompt is required.
Parameter | Description |
---|---|
prompt | Required. The text to display in the message box. |
buttons | Optional. The button and icon combination to display – see buttons table below. |
title | Optional. The text to display in the title bar. |
Helpfile & Context | Optional. The helpfile and content relating to the message box |
Button and Message Box Icon Combination
Use the following combinations of buttons, default button and icon settings when designing your message box. For example…
vbYesNo + vbDefaultButton2 + vbExclamation
Constant | Value | Description |
---|---|---|
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. |
vbCritical | 16 | Display Critical Message icon. |
vbQuestion | 32 | Display Warning Query icon. |
vbExclamation | 48 | Display Warning Message icon. |
vbInformation | 64 | Display Information Message icon. |
vbDefaultButton1 | 0 | First button is default. |
vbDefaultButton2 | 256 | Second button is default. |
vbDefaultButton3 | 512 | Third button is default. |
vbDefaultButton4 | 768 | Fourth button is default. |
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. |
MsgBox Example
The following code would create the message box shown below.
MsgBox "This action can't be undone. Do you wish to Continue?", _ vbYesNo + vbDefaultButton2 + vbExclamation, _ "Salary Increase"
Return Values
To do anything with the user’s response to a message box (which button they press) you can assign the return value to a variable. See the table of return values below.
Constant | Value | Description |
---|---|---|
vbOK | 1 | OK |
vbCancel | 2 | Cancel |
vbAbort | 3 | Abort |
vbRetry | 4 | Retry |
vbIgnore | 5 | Ignore |
vbYes | 6 | Yes |
vbNo | 7 | No |
In this example the same message box as shown above is assigned to the variable MsgBoxResult. You need to place brackets around the MsgBox parameters in this context. Only if the user clicks the Yes button is the code executed.
Sub SalaryIncrease() Dim SalaryCell As Range, SalaryField As Range Dim MsgBoxResult As Integer Const SalaryIncrease As Single = 1.05 Set SalaryField = Range("B2", Range("B2").End(xlDown)) MsgBoxResult = MsgBox( _ "This action can't be undone. Do you wish to Continue?", _ vbYesNo + vbDefaultButton2 + vbExclamation, "Salary Increase") If MsgBoxResult = vbYes Then For Each SalaryCell In SalaryField SalaryCell = SalaryCell * SalaryIncrease Next SalaryCell End If End Sub
The code above relates to the data shown below.
You don’t have to assign the MsgBox to a variable – see an alternative version of the code below.
Sub SalaryIncreaseWithoutVariable() Dim SalaryCell As Range, SalaryField As Range Const SalaryIncrease As Single = 1.05 Set SalaryField = Range("B2", Range("B2").End(xlDown)) If MsgBox("This action can't be undone. Do you wish to Continue?", _ vbYesNo + vbDefaultButton2 + vbExclamation, "Salary Increase") = vbYes Then For Each SalaryCell In SalaryField SalaryCell = SalaryCell * SalaryIncrease Next SalaryCell End If End Sub