Stop an Excel VBA Macro When the User Selects No
Question details
Stop an Excel VBA macro when the user selects No in a confirmation message.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A macro asks for confirmation before inserting rows, copying data, or deleting shapes.
- Observed behavior
- The macro should exit before changing the worksheet when the user selects No.
Locate the macro that displays the confirmation message and identify the worksheet-editing statements that follow it. Open the VBA editor with Alt+F11 and find the relevant procedure.
Exit the VBA procedure when the response is No
Use the message box result to stop the macro before it modifies worksheet data.
In the VBA editor, open the module containing the macro and locate its MsgBox confirmation prompt.
Store the result and exit the procedure if the user selects No: Dim Response As VbMsgBoxResult Response = MsgBox("Do you want to continue?", vbYesNo + vbQuestion, "Confirm") If Response = vbNo Then Exit Sub
Keep the code that inserts rows, copies data, or deletes shapes below the If statement. Run the macro and select No to verify that those actions do not occur.
Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface and Excel format compatibility, making it easy to move spreadsheet work from Microsoft Office.

Frequently Asked Questions
Can I check the message box response without a separate variable?
Yes. Use If MsgBox("Do you want to continue?", vbYesNo + vbQuestion) = vbNo Then Exit Sub before the worksheet-editing code.
What if the confirmation box also has a Cancel button?
Use vbYesNoCancel for the MsgBox buttons and check for vbYes instead: If Response <> vbYes Then Exit Sub. Both No and Cancel will stop the procedure.
Will Exit Sub stop a macro that called this procedure?
No. Exit Sub stops only the current Sub. If the calling procedure must stop too, return the response to it and check the result before continuing.
Where should I put the response check?
Place the If statement immediately after the MsgBox call and before any statements that insert rows, copy data, delete shapes, or make other unwanted changes.




