VBA MsgBox OK: Syntax, Buttons, and Examples

VBA & macros · Updated November 18, 2026

Yes—VBA’s MsgBox can display a simple OK message, and OK is the default button when you do not specify another button layout. Use it to show information or request a basic response; if you need to know which button someone clicked, capture the function’s return value.

What VBA MsgBox Does

MsgBox is a built-in Visual Basic for Applications function. In Excel, a macro can use it to pause and show a small dialog with text, an optional title, an icon, and one or more standard buttons. It is useful for brief notices such as “The report is ready,” and for simple choices such as Yes or No.

A message box is modal: the running macro waits until the user dismisses the dialog. That makes it useful when a person must see a warning before the procedure continues. It also means a message box can interrupt an automated process, so it is better suited to interactive macros than to unattended tasks.

The word “OK” can refer to two different things: the visible OK button, or the VBA constant that identifies the button result. vbOKOnly sets the dialog’s button layout to OK only. vbOK is the return value VBA uses when the user chooses OK. The similar names are easy to mix up, but they serve different purposes.

MsgBox Syntax and Its Arguments

The general form is MsgBox(prompt[, buttons][, title][, helpfile, context]). The prompt is the text shown in the dialog. The other arguments are optional, and the buttons argument can combine a button layout, an icon, a default-button choice, and other supported options.

  • prompt: The message to display. It can be a string or an expression that produces a string.
  • buttons: Constants that select the buttons and, optionally, an icon or default button. If omitted, the dialog has an OK button.
  • title: Text displayed in the dialog’s title bar. If omitted, VBA supplies a default title.
  • helpfile and context: Optional help information for a Help button. These arguments are rarely needed for ordinary Excel prompts.

For example, MsgBox "The import is complete." displays a message with the default OK button. To make the intent explicit and add an information icon, use MsgBox "The import is complete.", vbOKOnly Or vbInformation, "Import status". The Or operator combines the button and icon options.

Displaying a Simple OK Message

For a notice that requires no decision, an OK-only dialog is usually the clearest choice. This Excel VBA example displays a message, then writes the result to the Immediate window in the Visual Basic Editor. The result is not needed for the notice itself, but it demonstrates that the function returns a value.

Sub ShowReadyMessage()
    Dim response As VbMsgBoxResult

    ' Show a notice with one available button.
    response = MsgBox("The report is ready.", vbOKOnly Or vbInformation, "Complete")
    Debug.Print response
End Sub

Before running a macro, save a copy of the workbook: macro actions cannot be undone with Ctrl+Z. In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, insert a standard module if needed, and run the procedure from the editor or through Excel’s macro controls. Save a workbook containing VBA in a macro-enabled format such as .xlsm if you want to keep the code.

Basic MsgBox calls also work in Excel for Mac, although the dialog’s appearance follows the platform. Excel for the web does not run VBA macros, so a user must open the workbook in a desktop version of Excel to run this code.

Getting the User’s Button Choice

MsgBox is a function, so it returns a value that identifies the button selected. To make use of that value, place the call in an assignment or condition. The return value is an integer represented by the VbMsgBoxResult enumeration, which makes a variable declared with that type a readable choice in VBA.

For an OK/Cancel question, the result is typically vbOK or vbCancel. Test the result explicitly instead of assuming that a person accepted the prompt. This pattern is appropriate for a step where cancellation can safely stop the macro:

Sub ConfirmImport()
    Dim response As VbMsgBoxResult

    ' Continue only when the user chooses OK.
    response = MsgBox("Import the selected records?", vbOKCancel Or vbQuestion, "Confirm import")

    If response = vbOK Then
        Debug.Print "Import approved"
    Else
        Debug.Print "Import cancelled"
    End If
End Sub

In this example, vbOKCancel controls which buttons appear, while vbQuestion selects an icon. The result comparison uses vbOK, not vbOKOnly. For a Yes/No question, use vbYesNo and compare the return value with vbYes or vbNo.

Choosing Buttons, Icons, and the Default

The button-layout constants include vbOKOnly, vbOKCancel, vbYesNo, vbYesNoCancel, vbRetryCancel, and vbAbortRetryIgnore. A layout should describe the actual decision. Use Yes and No for a yes-or-no choice, and include Cancel when the user needs a clear way to back out.

Icon constants include vbInformation, vbExclamation, vbCritical, and vbQuestion. An icon adds visual emphasis but does not change what the result means. Avoid using a warning or critical icon for a routine success notice, because that can make harmless messages seem alarming.

Constants such as vbDefaultButton1, vbDefaultButton2, and vbDefaultButton3 can select which button is initially highlighted when that button exists. For a choice with consequences, choose the default thoughtfully. Do not make a potentially destructive action the accidental default simply to reduce clicks. The button layout, icon, and default selection can be combined in the buttons argument with Or.

Making the Message Easier to Read

Keep prompts brief and specific. “Continue?” gives little context; “Continue importing 25 selected rows?” tells the user what the choice concerns. A useful title, such as “Confirm import” or “Export complete,” makes the dialog easier to recognize, especially when several macros can display prompts.

For a message with multiple lines, insert the VBA line-break constant vbCrLf. For instance, MsgBox "Export complete." & vbCrLf & "File: " & outputPath, vbOKOnly Or vbInformation, "Export status" joins a status line and a file path. Keep long explanations out of a message box; a worksheet, UserForm, or saved log is often more suitable for detailed information.

When a prompt includes a value from a cell or variable, check that the value is meaningful before displaying it. A message such as “Processed 0 records” may be accurate but could indicate that the selected range was empty. Explaining the relevant context in the prompt helps users distinguish a normal result from a problem.

Common Problems and Misconceptions

  • Using the layout constant as the result: vbOKOnly configures the buttons; compare the returned value with vbOK.
  • Expecting a message box to return text: Standard buttons return a button choice, not arbitrary text. Use an InputBox or a custom UserForm if the user needs to type a response.
  • Expecting custom button labels: The built-in MsgBox function offers standard button captions. It does not provide a supported argument for renaming OK to something like “Save.”
  • Forgetting that code waits: The macro does not proceed past the call until the dialog is dismissed. Repeated prompts inside a loop can therefore create many interruptions.
  • Seeing no dialog in Excel for the web: VBA does not run in the browser version of Excel. Open the workbook in desktop Excel to run the macro.

A message box is also not a substitute for robust error handling. It can tell a person that a problem occurred, but a macro that needs to recover should handle errors in code and, where appropriate, record useful details. Do not display sensitive data in a prompt that may be visible to someone looking at the screen.

When to Use an Alternative

Use MsgBox when a short message or a small, standard choice is enough. It is quick to write and requires no custom form design. An InputBox is more suitable when the macro needs a short text or numeric response, though the input still needs validation before the code relies on it.

Use a VBA UserForm when the interaction needs custom labels, several fields, validation, instructions, or a more controlled layout. For information that users should be able to review later, put the status on a worksheet or write it to a log rather than showing a dialog that disappears when dismissed. For a macro that runs without a person present, avoid interactive prompts and choose a predictable method of reporting completion or failure.

Frequently Asked Questions

Is OK the default button in VBA MsgBox?

Yes. If you omit the buttons argument, MsgBox displays an OK button. You can also specify vbOKOnly to make the one-button layout explicit.

What is the difference between vbOKOnly and vbOK?

vbOKOnly is a button-layout constant that tells the dialog to show only OK. vbOK is the result constant used to identify that the user selected OK.

How do I make VBA continue only after OK is clicked?

Assign the result of MsgBox to a VbMsgBoxResult variable, then test whether it equals vbOK. With an OK-only dialog, OK is the sole available response.

Can I change the text on the OK button?

No. The standard MsgBox function uses built-in button captions. Create a UserForm if the interface needs a custom button label or other custom controls.

Can VBA MsgBox run in Excel for the web?

No. Excel for the web does not execute VBA macros. Run the workbook in desktop Excel for Windows or Mac; basic VBA message boxes are available in both desktop environments.

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.