Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA MsgBox OK Cancel: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To show an OK/Cancel message box in Excel VBA, use MsgBox with the vbOKCancel buttons argument and capture the return value: If MsgBox("Continue?", vbOKCancel) = vbOK Then .... This lets your macro pause and ask the user before doing something irreversible, like deleting rows or overwriting data. The same pattern works for Yes/No and other button sets by changing the constant and the values you compare against.

What You'll Need

  • Excel (any modern desktop version) with macros enabled.
  • Access to the VBA editor: press Alt+F11 on Windows, or Fn+Option+F11 on Mac.
  • A workbook saved as .xlsm (macro-enabled) if you want to keep the code.
  • Basic familiarity with inserting a module and running a macro.

Step-by-Step

  1. Open the VBA editor. In Excel, press Alt+F11 (Windows) or Fn+Option+F11 (Mac). The editor opens in a separate window.
  2. Insert a module. In the VBA editor, go to Insert > Module. A blank code window appears. (Menu labels can differ slightly by version and platform.)
  3. Write a basic OK/Cancel prompt. Type this procedure:
    Sub AskOKCancel()
        Dim answer As VbMsgBoxResult
        answer = MsgBox("Delete the selected rows?", vbOKCancel, "Confirm")
        If answer = vbOK Then
            MsgBox "You clicked OK."
        Else
            MsgBox "You clicked Cancel."
        End If
    End Sub
    The first argument is the message text, the second sets the buttons, and the third is the title bar text.
  4. Run the macro. Place the cursor inside the procedure and press F5, or go back to Excel and use Developer > Macros, select AskOKCancel, and click Run. If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon to enable it.
  5. Check the return values. Click OK once and Cancel once. The message confirms which button was pressed. vbOK equals 1 and vbCancel equals 2, but always compare against the named constants rather than numbers for readability.
  6. Add an icon (optional). Combine the buttons constant with an icon using +: MsgBox("Continue?", vbOKCancel + vbQuestion, "Confirm"). Other icons include vbExclamation, vbCritical, and vbInformation.
  7. Set a default button (optional). Add vbDefaultButton2 to make Cancel the default so pressing Enter does not accidentally confirm: MsgBox("Continue?", vbOKCancel + vbQuestion + vbDefaultButton2, "Confirm").
  8. Use the result to guard an action. Wrap the risky code in an If block so it only runs on OK:
    If MsgBox("Clear the data?", vbOKCancel + vbExclamation, "Confirm") = vbOK Then
        Range("A2:D100").ClearContents
    End If
    This is the core pattern: the message box returns a value, and that value controls whether the destructive line executes.
  9. Save the workbook as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). A standard .xlsx file discards the VBA code when saved.

Common Mistakes

  • Comparing to the wrong constant. Using vbYes with vbOKCancel never matches; the OK button returns vbOK. Match the constant to the button set you chose.
  • Forgetting to capture the result. Calling MsgBox "Continue?", vbOKCancel as a statement shows the dialog but ignores the answer, so the code continues regardless. Assign it to a variable or use it directly in an If.
  • Using + with text arguments incorrectly. The + operator combines numeric constants for buttons and icons; do not try to concatenate strings with it in the same argument list. Use & for string concatenation in the message text.
  • Assuming Cancel stops everything. Cancel only returns a value; your code must explicitly skip the action. Without the If check, Cancel does nothing useful.
  • Saving as .xlsx. The macro disappears on close. Save as .xlsm.
  • Confusing MsgBox with InputBox. InputBox collects text; MsgBox only reports and returns a button choice.

Tips

  • Keep message text short and specific: say what will happen, not just "Are you sure?"
  • Use vbDefaultButton2 for destructive actions so Enter does not confirm by accident.
  • Store the result in a VbMsgBoxResult variable when you need to check it more than once.
  • For Yes/No prompts, use vbYesNo and compare to vbYes; for three-way choices, use vbYesNoCancel and handle vbCancel separately.
  • Test both branches: click OK and Cancel each time you change the logic.
  • Avoid message boxes inside tight loops; they block execution and annoy users.

Troubleshooting

  • The macro does not appear in the list. Confirm the code is in a standard module (not a sheet or ThisWorkbook module) and that macros are enabled in Trust Center settings.
  • Compile error on VbMsgBoxResult. Check spelling; the type is VbMsgBoxResult. Alternatively declare the variable as Long or Integer.
  • The dialog never shows. The procedure may not be running, or DisplayAlerts is off. Re-enable alerts with Application.DisplayAlerts = True.
  • Both buttons do the same thing. Your If condition is probably always true or the comparison uses the wrong constant. Step through with F8 to inspect the returned value.
  • Code lost after saving. The file was saved as .xlsx. Re-add the code and save as .xlsm.
  • Mac vs Windows differences. The VBA editor shortcut and some dialog styling differ, but MsgBox constants behave the same.

Frequently Asked Questions

What value does OK return in a VBA MsgBox?

OK returns vbOK (1). Cancel returns vbCancel (2). Compare against the constants, not the numbers.

How do I make Cancel the default button?

Add vbDefaultButton2 to the buttons argument, for example vbOKCancel + vbDefaultButton2.

Can I change the button labels from OK and Cancel?

No. MsgBox uses fixed system labels. For custom button text you need a custom UserForm with your own buttons.

Why does my macro keep running after I click Cancel?

Because the return value was not checked. Assign the result and wrap the action in If ... = vbOK Then.

What is the difference between vbOKCancel and vbYesNo?

vbOKCancel shows OK and Cancel and returns vbOK or vbCancel; vbYesNo shows Yes and No and returns vbYes or vbNo. Use the constant that matches the wording you want.

Does MsgBox work on Excel for Mac?

Yes. The constants and return values are the same; only the editor shortcut and dialog appearance differ.