VBA MsgBox OK Button: Syntax, Examples, and Choices

VBA & macros · Updated November 18, 2026

To show an OK-only message box in Excel VBA, use MsgBox "Your message", vbOKOnly, "Your title". The OK button is also the default, so MsgBox "Your message" works when you do not need to set a title or other options.

What the OK Button Means in VBA

MsgBox displays a dialog box with a message and one or more buttons. In the simplest case, the dialog contains a single OK button. The user dismisses it by clicking OK or, in many cases, pressing Enter or Esc. A message box is useful when a macro needs to report a result, explain a problem, or pause for a user to acknowledge information.

The button choice and the value returned by the function are related but distinct. vbOKOnly specifies that the dialog should show only an OK button. If your code checks which button the user selected, the OK button’s return value is vbOK. These are built-in VBA constants, not text labels to type into the dialog.

For an OK-only dialog, checking the return value is usually unnecessary: there is no alternative button to choose. A return-value check matters when the dialog offers a choice, such as OK and Cancel or Yes and No.

MsgBox Syntax and the OK-Only Option

The general syntax is MsgBox(prompt, buttons, title, helpfile, context). The prompt is the message text. The remaining arguments are optional: buttons controls the buttons, icon, default button, and modality; title sets the title-bar text; and helpfile and context connect the dialog to a help file.

For an OK-only dialog, the relevant button constant is vbOKOnly, whose value is 0. It is also the default button setting. A concise example with a title is:

Sub ShowOKMessage()
    ' Save a copy of the workbook before running a macro.
    Dim result As VbMsgBoxResult

    result = MsgBox("The report is ready.", vbOKOnly, "Complete")
End Sub

The variable is declared here to show that MsgBox returns a result, but the code does not need to use result when OK is the only available choice. If you are not checking the return value, you can call MsgBox without assigning it. VBA permits both forms.

Examples for Common Excel Messages

Use a short, specific prompt that tells the reader what happened. A message that says “Done” may be unclear if the workbook contains several buttons or processes. You can include a line break with vbCrLf, or use the vbNewLine constant, to separate information into readable lines.

Sub ReportImport()
    ' Save a copy of the workbook before running a macro.
    Dim importedRows As Long
    Dim messageText As String

    importedRows = 24
    messageText = importedRows & " rows were imported." & vbCrLf & _
                  "Review the results on the Data sheet."
    MsgBox messageText, vbOKOnly + vbInformation, "Import finished"
End Sub

In this example, the prompt combines text with a number and a line break. The vbInformation constant adds an information icon, while vbOKOnly keeps the single OK button. The plus sign is commonly used to combine these option constants; Or is another clear choice for combining bit flags.

To refer to a worksheet value, build the prompt from the value rather than hard-coding a result. For example, a macro could place a count or a worksheet name in the message. Make sure the referenced worksheet and range actually exist; a MsgBox does not validate data or make a macro’s underlying operation safe.

Reading a Click and Choosing Other Buttons

When a user must make a decision, choose a button set that represents the available choices. Assign the function’s return value to a variable declared as VbMsgBoxResult, then compare it with a constant such as vbOK, vbCancel, vbYes, or vbNo. Avoid relying on the numeric values when named constants make the intention clearer.

Sub ConfirmAction()
    ' Save a copy of the workbook before running a macro.
    Dim answer As VbMsgBoxResult

    answer = MsgBox("Continue with the update?", _
                    vbOKCancel + vbQuestion, "Confirm update")

    If answer = vbOK Then
        MsgBox "The update can continue.", vbOKOnly, "Confirmed"
    Else
        MsgBox "The update was cancelled.", vbOKOnly, "Cancelled"
    End If
End Sub

Here, the first dialog offers OK and Cancel and adds a question icon. The second dialog only reports the branch taken, so it needs no choice check of its own. In a real macro, put the operation that should proceed inside the OK branch, and leave it out of the Cancel branch.

Common button groups include vbOKCancel, vbYesNo, and vbYesNoCancel. Pick one whose labels accurately describe the consequences. Do not use Yes and No when the user needs an explicit Cancel option to abandon the action without choosing either answer.

Adding Icons and Setting the Default Button

The buttons argument can combine a button group with an icon and a default-button setting. For example, vbOKOnly + vbExclamation shows an OK button with a warning-style icon. Common icon constants are vbInformation, vbExclamation, vbCritical, and vbQuestion. The icon should match the message: an information symbol is appropriate for a routine completion notice, while a critical icon should be reserved for serious errors.

When a dialog has several buttons, you can specify which one is selected by default using constants such as vbDefaultButton1 or vbDefaultButton2. This affects the initial focus; it does not remove other buttons or determine the user’s final choice. Take care with a destructive action: setting the safer option as the default can reduce the chance of an accidental confirmation.

Button captions themselves are fixed. The built-in MsgBox function cannot rename OK to “Save changes” or add a custom third button. If the task needs custom labels, several fields, or a more detailed confirmation screen, a VBA UserForm is a better fit.

Common Problems and How to Fix Them

  • The code will not compile: Check that the line uses MsgBox, with the correct spelling and balanced parentheses if you use them. For a multiline statement, each continued line except the last must end with a space and underscore.
  • The dialog has the wrong buttons: Confirm that the intended button-group constant is in the buttons argument. Remember that the first argument is the prompt; putting a title there makes the title appear as the message instead.
  • The result check behaves unexpectedly: Compare the returned value with the matching constant. An OK-only message always returns vbOK when dismissed, so it cannot distinguish between different decisions.
  • The title is missing: The title is a later optional argument. To provide it while also setting buttons, include both arguments in order, for example MsgBox "Saved", vbOKOnly, "Status".
  • The dialog appears behind another window: A MsgBox can be modal, meaning the user must dismiss it before continuing in the relevant application context. Window focus and other open dialogs can affect where it appears; close or resolve existing dialogs and keep prompts brief.

For text that includes quotation marks, double the quotation mark inside a VBA string. For instance, the string "Select ""Continue"" to proceed." displays the word Continue in quotation marks. This is a common source of syntax errors when prompts contain copied text.

When to Use MsgBox and When to Choose an Alternative

MsgBox is suited to short, occasional notices and simple decisions. It is quick to add to a macro and needs no separate form design. It is less suitable for messages that must remain visible, contain extensive instructions, show a table, collect typed input, or provide custom controls. A dialog also interrupts the user until it is dismissed, so avoid showing one for every row or routine step in a large automated process.

For a simple text response, VBA’s InputBox function can ask the user to enter a value, though it offers less control than a custom form. For a richer workflow, use a UserForm with labels, text boxes, and buttons. For progress during a long-running macro, updating the Excel status bar may be less disruptive than repeatedly opening message boxes.

If you need custom wording on a button, a UserForm is the appropriate alternative; changing a MsgBox’s button captions is not a supported option. Choose the least complicated interface that communicates the task clearly, and do not use a dialog as a substitute for validating data or handling errors in the macro.

Running VBA Safely in Excel

In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, or select Developer > Visual Basic if the Developer tab is shown. On a Mac, use Option+F11 (some keyboards also require the Fn key) or choose Developer > Visual Basic. The VBA editor and macro support are desktop features; Excel for the web can open macro-enabled workbooks but does not run VBA macros.

To keep VBA code in a workbook, save it in a macro-enabled format such as .xlsm. Excel’s macro security settings may block code from an untrusted file. Only enable macros when you trust the workbook’s source and understand what its code will do. A MsgBox may merely display text, but the macro that calls it can also change cells, delete data, or perform other actions.

Save a copy of the workbook before running any macro that changes data. VBA actions generally cannot be reversed with Ctrl+Z, and a confirmation dialog does not create an undo record. Test code on a copy first, especially if it will run across many worksheets or alter important records.

Frequently Asked Questions

How do I show only an OK button in VBA?
Use MsgBox "Message", vbOKOnly. Since OK-only is the default, MsgBox "Message" also displays one OK button.

What is the VBA constant for the OK button?
vbOKOnly sets the dialog’s button group to OK only. vbOK is the return value that identifies the OK response.

Does MsgBox need parentheses in VBA?
When you call it as a statement without using its return value, you can write MsgBox "Hello" without parentheses. When assigning the return value, use parentheses around the arguments, as in answer = MsgBox("Continue?", vbOKCancel).

Can I change the text on the OK button?
No. The built-in MsgBox uses standard button labels. Use a VBA UserForm if you need a button with custom wording or a more tailored dialog.

What does MsgBox return when it has only OK?
It returns vbOK when the user dismisses the dialog. Because there is only one choice, checking that result usually adds no useful branching.

Can Excel for the web run a VBA MsgBox?
No. Excel for the web does not execute VBA macros. Open the macro-enabled workbook in the Excel desktop application to run its VBA code, subject to macro security settings.

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.