VBA MsgBox: Create an OK or Decline Box?

VBA & macros · Updated November 3, 2026

VBA MsgBox: Create an OK or Decline Box?

Yes. In VBA, use MsgBox with the vbOKCancel button style to show OK and Cancel, or vbYesNo to show Yes and No; the built-in dialog cannot rename a button to “Decline.” If the button must literally say Decline, create a custom UserForm instead.

What “OK or decline” means in a VBA message box

Excel’s built-in MsgBox function offers a fixed set of button captions. It does not provide a parameter for supplying custom labels such as “Accept” and “Decline.” The closest built-in choice depends on what the two responses mean:

  • OK and Cancel: Use vbOKCancel when the user is confirming a notice or choosing whether to continue. The visible choices are OK and Cancel.
  • Yes and No: Use vbYesNo when the prompt is a direct question, such as whether to proceed. You can treat No as declining, but the visible button still says No.

That distinction matters for usability. A prompt that asks “Do you want to save these changes?” naturally fits Yes and No. A message such as “Review the information, then choose whether to continue” may fit OK and Cancel. If users need to see the word Decline, a custom form is the appropriate solution.

How MsgBox works

MsgBox displays a dialog and, when assigned to a variable, returns a number identifying the button the user selected. The first argument is the message. The optional second argument controls the buttons, icon, default button, and modality; a third optional argument supplies the dialog title. The button and icon options are named VBA constants, which makes the code easier to read than a numeric value.

For example, the following macro asks whether to continue. It uses Yes and No buttons, a question-mark style icon, and No as the default button:

Sub AskWhetherToContinue()
    Dim answer As VbMsgBoxResult

    ' Ask a direct question; make No the default choice.
    answer = MsgBox("Do you want to continue?", _
                    vbYesNo + vbQuestion + vbDefaultButton2, _
                    "Confirm")

    If answer = vbYes Then
        MsgBox "Continuing.", vbInformation, "Status"
    Else
        MsgBox "The action was declined.", vbInformation, "Status"
    End If
End Sub

The returned result is compared with vbYes, not with the displayed text or an assumed number. In this example, the No branch is described in the program as a decline, even though the dialog itself displays No. If the user closes the dialog with its close control or presses Esc, the result for a Yes/No box is treated as No.

Creating an OK and Cancel prompt

To show the literal built-in captions OK and Cancel, change the button style to vbOKCancel and compare the returned value with vbOK. Here is a complete example:

Sub AskForConfirmation()
    Dim answer As VbMsgBoxResult

    ' Save a copy of the workbook before running any macro.
    answer = MsgBox("Continue with this operation?", _
                    vbOKCancel + vbQuestion + vbDefaultButton2, _
                    "Confirmation")

    If answer = vbOK Then
        MsgBox "The operation can continue.", vbInformation, "Status"
    Else
        MsgBox "The operation was canceled.", vbInformation, "Status"
    End If
End Sub

vbDefaultButton2 makes the second button, Cancel, the default selection. It does not automatically cancel the operation: the macro still has to inspect the result and take the correct branch. Choosing a conservative default is useful when an accidental confirmation could have an unwanted effect, but the prompt should still explain what the user is deciding.

Before running a macro, save a copy of the workbook. VBA actions cannot be undone with Ctrl+Z, so a confirmation prompt is not a replacement for a backup or for careful testing.

Choosing the right buttons, icon, and default

Button styles and icons are combined with the plus operator. Common button constants include vbOKOnly, vbOKCancel, vbYesNo, vbYesNoCancel, and vbRetryCancel. Common icons include vbInformation, vbQuestion, vbExclamation, and vbCritical. The icon should match the message rather than being added decoratively.

  • Use vbQuestion for a genuine choice or confirmation.
  • Use vbExclamation to draw attention to a caution, not to imply that an ordinary choice is an error.
  • Use vbCritical for a serious problem that needs attention.
  • Use vbInformation for a neutral status or completion message.
  • Choose vbDefaultButton1, vbDefaultButton2, or another supported default deliberately. The default button is the one activated by pressing Enter.

A practical prompt states the consequence clearly. “Delete the selected records?” is more informative than “Are you sure?” If the operation affects many rows, mention that fact and, where feasible, describe the scope. Avoid using a Yes/No question when both choices are ambiguous; users should be able to tell what Yes and No will do.

Handling the result safely

Store the function’s return value in a variable declared as VbMsgBoxResult. This makes the intended result type clear and supports comparisons with constants such as vbCancel, vbNo, and vbRetry. Do not assume that a message box merely pauses the macro: after it closes, VBA continues at the next statement unless the code branches or exits.

For a destructive operation, put the action only in the confirmed branch. For example, check for vbYes before calling the code that deletes or overwrites data. A safer design also limits what the operation can affect, checks that the expected workbook or range is active, and gives the user an opportunity to review important changes. A prompt can reduce accidental clicks, but it is not an access-control feature and cannot guarantee that the user understands the consequences.

With vbOKCancel, test for vbOK to proceed; treat every other result as cancellation. With vbYesNoCancel, handle all three outcomes explicitly if they have different meanings. This is especially important when Cancel should leave a workflow untouched rather than being treated the same as No.

When a custom UserForm is needed

A VBA UserForm is the better option when the dialog needs buttons with exact captions such as Accept and Decline, more than a short message, or additional controls such as a checkbox or text field. Create a form in the Visual Basic Editor, add command buttons, set their Caption properties, and write event procedures for their clicks. The Accept button can set a result or continue a workflow; the Decline button can close the form while recording a different result.

A UserForm takes more code and needs more testing than a single MsgBox. Consider keyboard navigation, a sensible default, and what happens when the user closes the form without clicking either button. For a simple two-choice confirmation where standard Yes and No labels are acceptable, a built-in message box is usually quicker and easier to maintain. Use a custom form when the wording or layout is important enough to justify the extra work.

Excel compatibility and running the macro

VBA macros run in the desktop editions of Excel, including Excel for Microsoft 365 on Windows. They do not run in Excel for the web, so a workbook opened in a browser cannot execute this macro there. Excel for Mac supports VBA, though dialog appearance can differ from Windows and some other VBA features or integrations may vary. The examples here use standard MsgBox constants.

In desktop Excel for Windows, the Visual Basic Editor can be opened with Alt+F11, or from Developer > Visual Basic if the Developer tab is visible. To show that tab, use File > Options > Customize Ribbon and select Developer. On Mac, use the Visual Basic command on the Developer tab; keyboard shortcuts can vary with the keyboard and macOS settings. Save a macro-enabled workbook as an Excel Macro-Enabled Workbook (.xlsm) if it must retain VBA code, and follow your organization’s macro security rules. Do not enable macros in an untrusted file just to see its dialog.

Common mistakes and misconceptions

  • Expecting a “Decline” caption: Built-in MsgBox buttons have standard captions. Use No as the decline choice or build a UserForm for custom wording.
  • Ignoring the return value: Displaying a prompt alone does not stop later statements. Assign its result and branch on it.
  • Using the wrong constant: Compare a Yes/No result with vbYes or vbNo; compare an OK/Cancel result with vbOK or vbCancel.
  • Assuming the default makes a decision: The default only sets which button is initially selected. It does not choose the button for the user.
  • Treating Cancel and No as interchangeable: They can communicate different intentions. Decide whether Cancel means stop without making a choice, and code that outcome accordingly.

Frequently Asked Questions

Can I make a VBA MsgBox button say Decline?
No. The built-in dialog uses fixed button captions. Use a UserForm with a command button whose Caption is Decline if that exact word must appear.

How do I make an OK or Cancel box in Excel VBA?
Call MsgBox with vbOKCancel, save the return value, and test whether it equals vbOK. Treat the other response as cancellation.

How do I make No mean decline?
Use vbYesNo and write the prompt so the choices are clear. In the code, proceed only when the result equals vbYes; handle vbNo as declining.

Can I change the default button?
Yes. Combine the button style with a constant such as vbDefaultButton2. That selects the second button as the default; it does not remove the need to check the returned result.

Will this message box work in Excel for the web?
No. Excel for the web does not execute VBA macros. Run the workbook in desktop Excel, or choose a different approach that works in the browser.

Bottom line

For standard wording, use vbOKCancel for OK and Cancel or vbYesNo when No represents declining. Capture and check the result before allowing the macro to continue. If users must see a button labeled Decline, use a VBA UserForm rather than trying to rename a built-in MsgBox button.

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.