VBA & Macro Problems

Stop an Excel VBA Macro When the User Selects No

Elise WilliamsElise Williams Nov 27, 2026 870 views

Question details

Stop an Excel VBA macro when the user selects No in a confirmation message.

Stop an Excel VBA Macro When the User Selects No
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.
Before you start

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.

Solution 1Recommended

Exit the VBA procedure when the response is No

Use the message box result to stop the macro before it modifies worksheet data.

1
Find the confirmation prompt

In the VBA editor, open the module containing the macro and locate its MsgBox confirmation prompt.

2
Check the response immediately

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

3
Place edits after the check

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.

Exit Sub stops the current procedure: If another procedure called this macro, that calling procedure continues after the call returns. Handle the response in the calling procedure too if it must also stop.
Free Microsoft Office alternative

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.

Open and edit spreadsheets in a familiar interfaceCompatible with Microsoft Excel formats such as XLSXReview worksheet data and changes in WPS SpreadsheetsUse a free, lightweight office suite
microsoft office alternative - wps 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.