Home > Macros > Confirmation Before Execution

Confirmation Before Execution

Often times when you execute a macro, the process isn’t easily undone. You may have a macro that deletes certain information, or completely reformats your worksheet. In either case, you want to be sure that your workbook’s users are absolutely certain that they are ready to execute the macro before running it. If the macro can be run by clicking on an object within the worksheet, an accidental click may cause unintended consequences.

The code below will produce a pop up box as soon as the macro is run. It will ask the user if they are certain that they want to run the macro, and give them the option to proceed, or cancel it right there. Think of it like when you’re about to delete a file from your computer and Windows asks “Are you sure you want to move this file to the Recycle Bin?”. Clicking “Yes” will delete the file, and “No” will cancel the process.

You can edit the text to be displayed in between the quotes “Do you want to run this macro?”. The code will produce two options “OK” and “Cancel”. If you want it to produce “Yes” and “No”, change the vbOKCancel to vbYesNo, and vbCancel to vbNo. Both will do the same thing, it just depends on how you want to present the question.

Copy all the code below. Paste it into your workbook’s Visual Basic editor, either under a Microsoft Excel Object or Module. Replace ** YOUR MACRO CODE HERE ** with your macro’s commands.

Need help? Use our nifty guide to help figure out how to install and use your macros.

Sub Confirm_Macro()
'
'MACROS BY EXCELZOOM.COM
'
If MsgBox("Do you want to run this macro?", vbOKCancel + vbQuestion) = vbCancel Then
Exit Sub
Else
** YOUR MACRO CODE HERE **
End If
End Sub

Categories: Macros Tags:
  1. August 9th, 2009 at 12:29 | #1

    I’d use a separate function to ask the user for confirmation, passing back an integer to determine which vbMsgBoxResult value was selected. Based on the result, I could either exit or display a message to the end user.

  1. No trackbacks yet.