MsgBox Function in Excel VBA

The MsgBox function

The VBA MsgBox function is an easy way to display information and to take simple input (like Yes, No, Cancel) from the users.

MsgBox function: Syntax

A simplified version of the MsgBox syntax is as follows:

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

MsgBox function: Parameters or Arguments

The arguments are defined as follows:

  • prompt: It is required. This text is displayed in the message box.
  • buttons: It is optional. Here we shall use some code such as vbYesNo, vbOKOnly in this place. According to this code, different buttons will appear in the message box. For example, see this MsgBox function in the GetAnswer macro:
    Sub GetAnswer()
        Ans = MsgBox("Process the monthly report?", vbYesNo)
        If Ans = vbYes Then MsgBox ("Some reports will be showed :)")
        If Ans = vbNo Then Exit Sub
    End Sub

    This code (vbYesNo) will show two buttons in the message box, one Yes button and the other one is No button.

    Alternatives of UserForm in Excel: InputBox & MsgBox Functions

    Yes No Button showed in the dialog box due to code vbYesNo in the MsgBox function.

  • title: It is optional. This text appears in the message box’s title bar.

MsgBox function: Example

MsgBox function can be used by itself like this one: MsgBox “Click OK to continue” (parenthesis not included in this way) or it can be assigned to a variable like the above one: Ans = MsgBox(“Process the monthly report?”, vbYesNo).

In the following VBA macro, MsgBox function is assigned to a variable. Then the variable can be used in an effective way.

Sub GetAnswer()
    Ans = MsgBox("Process the monthly report?", vbYesNo)
    If Ans = vbYes Then MsgBox ("Some reports will be showed :)")
    If Ans = vbNo Then Exit Sub
End Sub

The Ans variable will have two values corresponding to vbYes or vbNo. If the value is vbYes, it will work one work and if the value is vbNo then it will do different work.

The following Table lists the built-in constants that you can use for the buttons argument. You can display your preferred buttons, make visible an icon, and select which button will be the default.

MsgBox function: Constants

MsgBox TABLE: Constants Used in the MsgBox Function

vbOKOnly0It displays OK button.
vbOKCancel1It displays OK and Cancel buttons.
vbAbortRetryIgnore2It displays Abort, Retry, and Ignore buttons.
vbYesNoCancel3It displays Yes, No, and Cancel buttons.
vbYesNo4It displays Yes and No buttons.
vbRetryCancel5It displays Retry and Cancel buttons.
vbCritical16It displays Critical Message icon.
vbQuestion32It displays Query icon (a question mark).
VBExclamation48It displays Warning Message icon.
vbInformation64It displays Information Message icon.
vbDefaultButton10First button is default.
vbDefaultButton2256Second button is default.
vbDefaultButton3512Third button is default.

Observe the following VBA macro. I have used a combination of constants (vbYesNo, vbQuestion and vbDefaultButton2) to show a dialog box with a Yes button, a No button, and a Question mark icon. Default button (vbDefaultButton2) will be the second button (No button).

Sub GetAnswer()
    Config = vbYesNo + vbQuestion + vbDefaultButton2
    Ans = MsgBox("Process the monthly report?", Config)
    If Ans = vbYes Then MsgBox "You pressed to see the Monthly Report."
    If Ans = vbNo Then Exit Sub
End Sub

See the following figure.

Alternatives of UserForm in Excel: InputBox & MsgBox Functions

Showing Yes, No buttons and a question mark beside the Text.

MsgBox function: Download

Download this file to practice yourself.

Happy Excelling ☕

Read More…

MsgBox & InputBox in VBA Excel : UserForm Alternatives

InputBox Function in Excel VBA

Hello! Welcome to my Excel blog! It took me some time to be a fan of Excel. But now I am a die-hard fan of MS Excel. I learn new ways of doing things with Excel and share here. Not only how to guide on Excel, but you will get also topics on Finance, Statistics, Data Analysis, and BI. Stay tuned!

We will be happy to hear your thoughts

      Leave a reply