To create a Yes/No input box in Excel VBA, you use the MsgBox function with the vbYesNo argument. This allows the user to make a binary choice, and the code captures that choice as an integer value (6 for Yes, 1 for No), which you then use in an If...Then statement to trigger specific actions. Unlike a standard InputBox, which asks for text, a MsgBox provides predefined buttons for faster, error-free user interaction.

What You'll Need

  • Microsoft Excel installed on a Windows or macOS computer.
  • The Developer tab enabled in the Excel Ribbon.
  • A basic understanding of how to open the Visual Basic for Applications (VBA) editor.
  • An Excel workbook saved in the .xlsm (Excel Macro-Enabled Workbook) format to ensure the code is saved.

Step-by-Step

  1. Enable the Developer Tab: If you do not see the Developer tab at the top of your Excel screen, right-click anywhere on the Ribbon and select "Customize the Ribbon." In the right-hand list, check the box next to "Developer" and click OK.
  2. Open the VBA Editor: Click the "Developer" tab and then click the "Visual Basic" button (or press Alt + F11 on your keyboard). This opens the VBA environment where your scripts are written.
  3. Insert a New Module: In the VBA editor, go to the top menu and select Insert > Module. This creates a blank white space where you can write your macro code.
  4. Declare a Variable for the User Response: To store whether the user clicked "Yes" or "No," you need a variable. Use the VbMsgBoxResult data type, which is specifically designed to handle the return values of a message box. Type the following at the start of your subroutine: Dim userResponse As VbMsgBoxResult
  5. Write the MsgBox Code: Use the MsgBox function. You must wrap the function in parentheses because you are assigning its result to a variable. The syntax requires the prompt text, the button style, and an optional title. Example: userResponse = MsgBox("Do you want to clear the data?", vbYesNo + vbQuestion, "Confirmation Required") In this line, vbYesNo tells Excel to show only those two buttons, and vbQuestion adds a question mark icon to the box.
  6. Create the Logic Branch: Now, use an If...Then...Else statement to decide what happens based on the response. The constant vbYes represents the "Yes" button. Example: If userResponse = vbYes Then ' Code to execute if Yes is clicked MsgBox "Data has been cleared!" Else ' Code to execute if No is clicked MsgBox "Action cancelled." End If
  7. Complete the Subroutine: Wrap your code in a Sub and End Sub block. For example: Sub ConfirmAction() Dim userResponse As VbMsgBoxResult userResponse = MsgBox("Proceed with update?", vbYesNo + vbCritical, "Warning") If userResponse = vbYes Then Range("A1").Value = "Updated" Else MsgBox "No changes made." End If End Sub
  8. Run and Test: Return to your Excel sheet. You can run the macro by clicking "Macros" on the Developer tab, selecting "ConfirmAction," and clicking "Run." Alternatively, you can insert a button from the "Insert" menu under "Form Controls" and assign this macro to it.

Common Mistakes

One of the most frequent errors is forgetting to use parentheses around the MsgBox arguments. In VBA, if you are simply displaying a message, you don't need parentheses. However, if you are assigning the result to a variable (like userResponse = MsgBox(...)), the parentheses are mandatory. Without them, VBA will throw a "Compile error: Expected: =" or "Syntax error."

Another common mistake is using the wrong data type for the response variable. While a generic Variant or Integer will work, using VbMsgBoxResult is the professional standard because it explicitly tells anyone reading the code that this variable is intended for a message box result.

Users also frequently confuse InputBox with MsgBox. An InputBox is used when you need the user to type a specific string of text or a number. A MsgBox is used for alerts or binary choices. If you try to use vbYesNo inside an InputBox, the code will fail because InputBox does not support button customization in that manner.

Lastly, failing to save the file as an .xlsm is a critical error. If you save a workbook containing VBA code as a standard .xlsx file, Excel will strip out all the macros without warning, and your hard work will be permanently deleted upon closing the file.

Tips

To make your input boxes more professional, combine button constants with icon constants using the plus sign (+). For example, vbYesNo + vbExclamation will show the Yes/No buttons along with a warning triangle. This provides a visual cue to the user about the nature of the question (e.g., a question mark for general queries, an exclamation point for warnings, and a red X for critical errors).

If you find yourself writing the same confirmation box in multiple different macros, consider creating a custom function. You can write a single function that returns a Boolean (True or False) and call that function whenever you need a confirmation. This keeps your code clean and makes it easier to change the wording of your prompts across the entire project in one place.

For better accessibility, always provide a clear and concise title in the third argument of the MsgBox function. Instead of leaving it blank or using "Message," use descriptive titles like "Data Import Confirmation" or "Delete Record Warning." This helps users who may have multiple windows open understand exactly which process is requesting their attention.

Consider the "Default Button" setting. By default, the "Yes" button is highlighted. If the action you are asking about is dangerous (like deleting a whole sheet), you can use vbDefaultButton2. This shifts the focus to the "No" button, preventing users from accidentally hitting Enter and triggering a destructive action.

Troubleshooting

If your code is not running, the first thing to check is the Macro Security settings. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Ensure that "Disable all macros with notification" is selected. This allows you to enable macros manually when you open the file via the yellow security bar at the top of the screen.

If you receive a "Type Mismatch" error, check that you haven't accidentally assigned a string to your userResponse variable. Ensure you are using the constants vbYes and vbNo rather than typing the words "Yes" or "No" in quotes. The MsgBox function returns an integer, not a text string.

If the message box appears but the code inside the If...Then block doesn't execute, verify your logic. A common mistake is using a single equals sign for assignment instead of comparison, though VBA is generally forgiving with If statements. Double-check that you haven't accidentally placed an End Sub prematurely, cutting off the logic before it reaches the action steps.

If the VBA editor feels sluggish or crashes, try clearing your compiled code. You can do this by going to the Debug menu in the VBA editor and selecting "Compile VBA Project." This checks for syntax errors throughout the entire project and can often highlight the exact line causing a runtime failure.

Frequently Asked Questions

Can I add a third option like "Maybe" or "Cancel"?

The MsgBox function does not support a "Maybe" button, but it does support vbYesNoCancel. This adds a third button. In this case, you would need to add an ElseIf userResponse = vbCancel Then block to handle the cancellation logic separately from the "No" response.

Does this work on Excel for Mac?

Yes, the MsgBox function and vbYesNo constants are standard across both Windows and macOS versions of Excel VBA. However, the visual appearance of the dialog box will differ slightly to match the operating system's native UI style.

Can I change the text of the buttons from "Yes" and "No" to something else?

No, the standard MsgBox only allows the predefined buttons. If you need custom button text (e.g., "Save" and "Discard"), you must create a UserForm. A UserForm allows you to design a custom dialog box with any labels, buttons, and colors you prefer.

What happens if the user just closes the box using the 'X' button?

When using vbYesNo, the 'X' button is typically disabled or behaves as a "No" response depending on the version. However, if you use vbYesNoCancel, clicking the 'X' is treated as clicking the "Cancel" button, returning the vbCancel value.

Can I make the Yes/No box appear automatically when a cell is changed?

Yes, by placing the code inside a Worksheet_Change event instead of a standard module. This allows the macro to trigger automatically whenever a specific range of cells is edited, asking the user for confirmation before the change is finalized.