Smart Systems for Home

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

Excel VBA MsgBox: Set a Default Button

· 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

You set a default button in an Excel VBA MsgBox by adding a constant such as vbDefaultButton2 to the Buttons argument, so the second button is pre-selected when the dialog opens. For example, MsgBox "Save changes?", vbYesNo + vbDefaultButton2 highlights No, and pressing Enter chooses it. This matters because a MsgBox with Yes/No or OK/Cancel defaults to the first button unless you say otherwise, and a stray Enter can confirm the wrong action.

What You'll Need

  • Excel on Windows or Mac with the Visual Basic Editor available (Alt+F11 on Windows, or Developer > Visual Basic on the ribbon).
  • A macro-enabled workbook (.xlsm) if you want to keep the code.
  • Basic familiarity with inserting a module and running a macro.

Step-by-Step

  1. Open the Visual Basic Editor. In Excel, press Alt+F11 (Windows) or use Developer > Visual Basic (Mac). If the Developer tab is hidden, enable it in Excel Options or Excel Preferences. Menu labels can differ slightly by version, device or region.
  2. Insert a module. In the VBA editor, choose Insert > Module. A blank code window appears where you can write the macro.
  3. Start a Sub procedure. Type Sub ConfirmDelete() and press Enter. The editor adds End Sub automatically.
  4. Write the MsgBox line with the default button constant. The Buttons argument is a sum of constants. Use vbYesNo for Yes/No, vbOKCancel for OK/Cancel, or vbYesNoCancel for three buttons. Add vbDefaultButton1, vbDefaultButton2, or vbDefaultButton3 to choose which button is highlighted. For example: MsgBox "Delete this row?", vbYesNo + vbDefaultButton2. Here the second button (No) is the default, so pressing Enter does not delete.
  5. Capture the result if you need to act on it. Assign the MsgBox to a variable: Dim answer As VbMsgBoxResult then answer = MsgBox("Delete this row?", vbYesNo + vbDefaultButton2). Test with If answer = vbYes Then and put your delete code inside the block.
  6. Run the macro. Press F5 or use Run > Run Sub/UserForm. The dialog appears with the chosen button highlighted. Press Enter to confirm the default works as intended.
  7. Adjust for three-button dialogs. With vbYesNoCancel, vbDefaultButton3 highlights Cancel, which is a safe default for destructive prompts. Keep the constant names exact; VBA will not compile a misspelled constant.
  8. Save the workbook as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook, otherwise the code is lost when you close Excel.

Common Mistakes

  • Using the wrong constant number. vbDefaultButton1 is 0, vbDefaultButton2 is 256, and vbDefaultButton3 is 512. Typing 1, 2, or 3 instead of the named constant gives an unexpected button or an error.
  • Adding constants with the wrong operator. Use + between button constants, for example vbYesNo + vbDefaultButton2. Using & concatenates text and breaks the call.
  • Forgetting that the default is only a highlight. The user can still click another button; the default only decides what Enter does.
  • Putting the constant in the wrong argument. The default button constant belongs in the Buttons argument, not in Title or HelpFile.
  • Assuming the default applies to the X close button. Closing a MsgBox with the window X returns vbCancel (or the cancel equivalent), not the default button.

Tips

  • For destructive actions, set the default to No or Cancel, for example vbYesNo + vbDefaultButton2 or vbOKCancel + vbDefaultButton2.
  • Use a clear title in the third argument, such as MsgBox "Delete this row?", vbYesNo + vbDefaultButton2, "Confirm Delete".
  • Keep messages short and put the action in the title so the buttons are easy to read.
  • Test the macro with both Enter and mouse clicks to confirm the result variable behaves as expected.
  • If you need a custom button label, VBA's MsgBox cannot do it; use a UserForm instead.

Troubleshooting

  • The wrong button is highlighted. Check that you used the named constant and that it matches the button position: vbDefaultButton2 is the second button from the left, not the second word in the prompt.
  • Compile error: variable not defined. The constant name is misspelled or the module is missing Option Explicit requirements. Use the exact spelling from the VBA object browser.
  • Nothing happens when you press Enter. The default button may be disabled by the combination of constants; test with a simple vbOKOnly first, then add the default constant.
  • The macro does not run. Check that the code is in a standard module, not a sheet module, and that macros are enabled for the workbook.
  • The result is always the same. If you do not store the return value, the MsgBox still appears but your code cannot branch on the answer. Assign it to a variable and compare with vbYes, vbNo, or vbCancel.

Frequently Asked Questions

Can I set the default button to Cancel?

Yes. Use vbOKCancel + vbDefaultButton2 for a two-button OK/Cancel dialog, or vbYesNoCancel + vbDefaultButton3 for a three-button dialog where Cancel is the third button.

Does the default button change what the user can click?

No. It only sets which button is pre-selected when the dialog opens. The user can still choose any button with the mouse or keyboard.

What number is vbDefaultButton2?

vbDefaultButton2 equals 256. vbDefaultButton1 is 0 and vbDefaultButton3 is 512. Use the named constants rather than the numbers for readability.

Can I use a default button with a custom message and title?

Yes. Put the message first, the button constants second, and the title third: MsgBox "Continue?", vbYesNo + vbDefaultButton2, "Confirm".

Why does my MsgBox ignore the default button constant?

Check that the constant is in the Buttons argument and spelled exactly, and that you have not used a numeric value that conflicts with another button constant.

Can I make the default button different on Mac and Windows?

The VBA constants behave the same on both platforms for standard MsgBox dialogs, but the visual order of buttons can differ by system settings. Test on the target platform if the exact highlight position matters.