# Microsoft Office Application Help - Excel Help forum > Excel Programming / VBA / Macros >  >  Add message box to confirm run macro

## Anolan

Anyone...I would like to add a message box to my code that will prompt the
user with a "Yes" or "No" to confirm running a macro when the user clicks the
command button.  If YES, then the macro resumes; however, if NO, then the
macros stops.

Thanks in advance...Andy

----------


## JE McGimpsey

One way:

Dim nResult As Long
nResult = MsgBox( _
Prompt:="Really run this macro?", _
Buttons:=vbYesNo)
If nResult = vbYes Then
'Rest of macro macro
MsgBox "OK"
End If



In article <1AC1409C-6898-4ACC-BBE6-5BDC230F16AD@microsoft.com>,
Anolan <Anolan@discussions.microsoft.com> wrote:

> Anyone...I would like to add a message box to my code that will prompt the
> user with a "Yes" or "No" to confirm running a macro when the user clicks the
> command button.  If YES, then the macro resumes; however, if NO, then the
> macros stops.
>
> Thanks in advance...Andy

----------


## Curt D.

I tried this and the macro still runs even when you select NO. can anyone
help. Thanks

"JE McGimpsey" wrote:

> One way:
>
>         Dim nResult As Long
>         nResult = MsgBox( _
>             Prompt:="Really run this macro?", _
>             Buttons:=vbYesNo)
>         If nResult = vbYes Then
>             'Rest of macro macro
>             MsgBox "OK"
>         End If
>
>
>
> In article <1AC1409C-6898-4ACC-BBE6-5BDC230F16AD@microsoft.com>,
>  Anolan <Anolan@discussions.microsoft.com> wrote:
>
> > Anyone...I would like to add a message box to my code that will prompt the
> > user with a "Yes" or "No" to confirm running a macro when the user clicks the
> > command button.  If YES, then the macro resumes; however, if NO, then the
> > macros stops.
> >
> > Thanks in advance...Andy
>

----------


## JE McGimpsey

Did you put the rest of your macro where I put

'Rest of macro

, i.e., within the If...End If structure?

Or are you saying that the Msgbox "OK" line was executed even if you
clicked NO??


In article <E8E5ECB0-FF55-4D1E-847E-9EB2A704A554@microsoft.com>,
"Curt D." <CurtD@discussions.microsoft.com> wrote:

> I tried this and the macro still runs even when you select NO. can anyone
> help. Thanks

----------


## Dave Peterson

I think J.E. hit the nail on the head.  You didn't put your code in the correct
spot following his suggestion.

But maybe you could change the code just a bit:

Dim nResult As Long
nResult = MsgBox( _
Prompt:="Really run this macro?", _
Buttons:=vbYesNo)
If nResult = vbno Then
exit sub
End If

'rest of macro goes here now



Curt D. wrote:
>
> I tried this and the macro still runs even when you select NO. can anyone
> help. Thanks
>
> "JE McGimpsey" wrote:
>
> > One way:
> >
> >         Dim nResult As Long
> >         nResult = MsgBox( _
> >             Prompt:="Really run this macro?", _
> >             Buttons:=vbYesNo)
> >         If nResult = vbYes Then
> >             'Rest of macro macro
> >             MsgBox "OK"
> >         End If
> >
> >
> >
> > In article <1AC1409C-6898-4ACC-BBE6-5BDC230F16AD@microsoft.com>,
> >  Anolan <Anolan@discussions.microsoft.com> wrote:
> >
> > > Anyone...I would like to add a message box to my code that will prompt the
> > > user with a "Yes" or "No" to confirm running a macro when the user clicks the
> > > command button.  If YES, then the macro resumes; however, if NO, then the
> > > macros stops.
> > >
> > > Thanks in advance...Andy
> >

--

Dave Peterson

----------


## doubleeinc

Thank you! I was able to use this and it worked perfectly.

----------


## Colibri2

Hi All

I've been following this topic. Here's my program, but when I choose Yes, it doesn't execute the macro as it should. HAS to be User Error! I want it to NOT run if I choose No, and run if I choose Yes. Thank you in advance!

Sub ClearingWF_PracticeVersion()


Dim nResult As Long
nResult = MsgBox( _
Prompt:="Do you REALLY want to run this macro? It will DELETE all numbers you have input!", _
Buttons:=vbYesNo)
If nResult = vbNo Then
Exit Sub
End If

   Sheets("Practice Version").Range("B2:B3,C2:C3,D2:D3,E2:E3,D15:E16,E18,L15:L16,L19:L20,M15:M16").ClearContents
   ActiveWorkbook.Save

End Sub

----------

