VBA Message Box Example: MsgBox Syntax & Uses

VBA & macros · Updated November 23, 2026

The simplest VBA message box example is MsgBox "Hello World", which displays a dialog box with the text “Hello World” and an OK button. For more control, use the full syntax: MsgBox prompt, buttons, title, where buttons determines which buttons and icons appear and title sets the window caption.

What Is a Message Box in VBA?

A message box (MsgBox) is a built-in dialog box in Microsoft Access, Excel, Word, and other Office applications that displays a message to the user. It is part of the VBA language and is used to inform, warn, or ask the user for a simple decision. The MsgBox function returns a value indicating which button the user clicked, which you can capture in a variable to control program flow.

Basic Syntax and Arguments

The full syntax of the MsgBox function is:

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

Only prompt is required. The buttons argument is a numeric value or constant that specifies the buttons to display, the icon style, the default button, and modality. The title is the text in the title bar. The helpfile and context arguments are rarely used.

Common button constants include:

  • vbOKOnly (0) – OK button only (default)
  • vbOKCancel (1) – OK and Cancel
  • vbYesNo (4) – Yes and No
  • vbYesNoCancel (3) – Yes, No, and Cancel
  • vbRetryCancel (5) – Retry and Cancel

Icon constants include vbCritical (16), vbQuestion (32), vbExclamation (48), and vbInformation (64). You can combine a button constant and an icon constant by adding them, for example vbYesNo + vbQuestion.

Simple Message Box Examples

Here are three basic examples you can copy into a VBA module:

Sub ShowSimpleMessage()
    ' Displays a message with an OK button
    MsgBox "Hello, world!"
End Sub

Sub ShowMessageWithTitle()
    ' Displays a message with a custom title
    MsgBox "Please save your work.", vbExclamation, "Reminder"
End Sub

Sub AskYesNo()
    ' Asks a question and captures the user's choice
    Dim answer As VbMsgBoxResult
    answer = MsgBox("Do you want to continue?", vbYesNo + vbQuestion, "Confirm")
    If answer = vbYes Then
        MsgBox "You clicked Yes."
    Else
        MsgBox "You clicked No."
    End If
End Sub

In the third example, the answer variable stores the result of the MsgBox call. The constants vbYes (6) and vbNo (7) are returned by the function. You can also use vbOK (1) and vbCancel (2) for OK/Cancel boxes.

How to Insert and Run a MsgBox Macro

To use these examples in Excel (Microsoft 365 on Windows):

  1. Press Alt+F11 to open the VBA editor. On a Mac, press Fn+Option+F11 or go to Tools > Macro > Visual Basic Editor.
  2. In the Project Explorer, right-click on your workbook name and choose Insert > Module.
  3. Paste the code into the module window.
  4. Close the editor and press Alt+F8 (on Mac: Option+F8) to open the Macro dialog, select your macro, and click Run.

Before running any macro, save a copy of your workbook (File > Save As) because macros cannot be undone with Ctrl+Z. Also, if your workbook contains sensitive data, be aware that the macro runs with your permissions.

Using MsgBox Return Values in Code

The MsgBox function returns an integer that tells you which button was clicked. You can use this to branch your code. The most common return values are:

  • vbOK (1)
  • vbCancel (2)
  • vbYes (6)
  • vbNo (7)

Here is an example that loops until the user clicks Yes:

Sub LoopUntilYes()
    Dim response As VbMsgBoxResult
    Do
        response = MsgBox("Do you want to exit?", vbYesNo, "Exit Loop")
    Loop While response <> vbYes
    MsgBox "Loop ended."
End Sub

Note that the VbMsgBoxResult data type is an enumeration; you can also declare the variable as Long or Integer.

Common Problems and How to Avoid Them

One frequent issue is forgetting that the MsgBox halts code execution until the user responds. This is intentional, but if you have a long-running macro, a message box can freeze the application if you forget to handle it. Another problem is using the wrong constant combination; for example, vbYesNo + vbDefaultButton2 (256) makes the second button the default, which can lead to accidental selections if the user presses Enter.

Another common mistake is using a message box in a loop that runs many times, causing the user to click OK repeatedly. Instead, consider using the Application.StatusBar to show progress or writing to a log file.

If you need to display a message box that does not block the user (non-modal), VBA does not have a built-in non-modal MsgBox. You would need to create a UserForm for that.

Alternatives to MsgBox

For more interactive input, you can use the InputBox function, which asks the user to type a value. For more complex dialogs with multiple controls, create a UserForm. For logging or debugging, use Debug.Print to output to the Immediate window instead of interrupting the user.

Frequently Asked Questions

Can I change the font or size of a message box?

No. The MsgBox uses the system font and cannot be customized. To change appearance, you must create a UserForm with a label or text box.

How do I add a line break in a message box?

Use the VBA newline character vbCrLf (carriage return + line feed) or vbNewLine. For example: MsgBox "Line1" & vbCrLf & "Line2".

Why does my message box show “Microsoft Excel” as the title?

If you omit the title argument, the application name (e.g., “Microsoft Excel”) is used by default. To change it, provide a title string, like MsgBox "Hello", , "My Custom Title".

Can I make a message box with a text input field?

No, the MsgBox only shows buttons and text. Use InputBox for a simple text input, or a UserForm for more complex input.

How do I display a message box after a certain time?

There is no built-in timer for MsgBox. You can use the Application.OnTime method to schedule a macro that shows the message box after a delay.

Is MsgBox available in all Office applications?

Yes, MsgBox is part of the VBA language and works in Excel, Word, Access, PowerPoint, and Outlook (in VBA modules). It also works in the Visual Basic Editor itself.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.