Excel VBA MsgBox OK Cancel: Step-by-Step Guide
In this article
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+F11on Windows, orFn+Option+F11on 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
- Open the VBA editor. In Excel, press
Alt+F11(Windows) orFn+Option+F11(Mac). The editor opens in a separate window. - 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.) - Write a basic OK/Cancel prompt. Type this procedure:
The first argument is the message text, the second sets the buttons, and the third is the title bar text.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 - Run the macro. Place the cursor inside the procedure and press
F5, or go back to Excel and useDeveloper > Macros, selectAskOKCancel, and click Run. If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon to enable it. - Check the return values. Click OK once and Cancel once. The message confirms which button was pressed.
vbOKequals 1 andvbCancelequals 2, but always compare against the named constants rather than numbers for readability. - Add an icon (optional). Combine the buttons constant with an icon using
+:MsgBox("Continue?", vbOKCancel + vbQuestion, "Confirm"). Other icons includevbExclamation,vbCritical, andvbInformation. - Set a default button (optional). Add
vbDefaultButton2to make Cancel the default so pressing Enter does not accidentally confirm:MsgBox("Continue?", vbOKCancel + vbQuestion + vbDefaultButton2, "Confirm"). - Use the result to guard an action. Wrap the risky code in an
Ifblock so it only runs on OK:
This is the core pattern: the message box returns a value, and that value controls whether the destructive line executes.If MsgBox("Clear the data?", vbOKCancel + vbExclamation, "Confirm") = vbOK Then Range("A2:D100").ClearContents End If - Save the workbook as macro-enabled. Use
File > Save Asand choose Excel Macro-Enabled Workbook (.xlsm). A standard.xlsxfile discards the VBA code when saved.
Common Mistakes
- Comparing to the wrong constant. Using
vbYeswithvbOKCancelnever matches; the OK button returnsvbOK. Match the constant to the button set you chose. - Forgetting to capture the result. Calling
MsgBox "Continue?", vbOKCancelas a statement shows the dialog but ignores the answer, so the code continues regardless. Assign it to a variable or use it directly in anIf. - 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
Ifcheck, Cancel does nothing useful. - Saving as
.xlsx. The macro disappears on close. Save as.xlsm. - Confusing MsgBox with InputBox.
InputBoxcollects text;MsgBoxonly reports and returns a button choice.
Tips
- Keep message text short and specific: say what will happen, not just "Are you sure?"
- Use
vbDefaultButton2for destructive actions so Enter does not confirm by accident. - Store the result in a
VbMsgBoxResultvariable when you need to check it more than once. - For Yes/No prompts, use
vbYesNoand compare tovbYes; for three-way choices, usevbYesNoCanceland handlevbCancelseparately. - 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 isVbMsgBoxResult. Alternatively declare the variable asLongorInteger. - The dialog never shows. The procedure may not be running, or
DisplayAlertsis off. Re-enable alerts withApplication.DisplayAlerts = True. - Both buttons do the same thing. Your
Ifcondition is probably always true or the comparison uses the wrong constant. Step through withF8to 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
MsgBoxconstants 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.