Skip to content

Excel VBA Message Boxes – MsgBox Function

    MsgBox Parameters

    Here are the parameters for the MsgBox function.

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

    Only prompt is required.

    ParameterDescription
    promptRequired. The text to display in the message box.
    buttonsOptional. The button and icon combination to display – see buttons table below.
    titleOptional. The text to display in the title bar.
    Helpfile & ContextOptional. 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
    ConstantValueDescription
    vbOKOnly0Display OK button only.
    vbOKCancel1Display OK and Cancel buttons.
    vbAbortRetryIgnore2Display Abort, Retry, and Ignore buttons.
    vbYesNoCancel3Display Yes, No, and Cancel buttons.
    vbYesNo4Display Yes and No buttons.
    vbRetryCancel5Display Retry and Cancel buttons.
    vbCritical16Display Critical Message icon.
    vbQuestion32Display Warning Query icon.
    vbExclamation48Display Warning Message icon.
    vbInformation64Display Information Message icon.
    vbDefaultButton10First button is default.
    vbDefaultButton2256Second button is default.
    vbDefaultButton3512Third button is default.
    vbDefaultButton4768Fourth button is default.
    vbApplicationModal0Application modal; the user must respond to the message box before continuing work in the current application.
    vbSystemModal4096System modal; all applications are suspended until the user responds to the message box.
    vbMsgBoxHelpButton16384Adds Help button to the message box.
    VbMsgBoxSetForeground65536Specifies 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.

    ConstantValueDescription
    vbOK1OK
    vbCancel2Cancel
    vbAbort3Abort
    vbRetry4Retry
    vbIgnore5Ignore
    vbYes6Yes
    vbNo7No

    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