Message box with Yes or No option

M

Munchkin

I want to my 2nd message box (is the policy mod # is correct) to have a Yes
or No button - if yes is clicked, the macro proceeeds, if no is clicked
another message box should appear stateing "Find correct policy mod" and the
macro should stop running.

How do I do this? Thanks!

MY MACRO:
If Len(Application.Trim(Range("E5"))) < 1 Then
MsgBox "You have not entered a policy name"
Exit Sub
End If
'MsgBox "go on "

MsgBox "Is the Policy Mod # correct?"
 
B

Barb Reinhardt

Try adding this

Dim PolicyMod as VBMsgBoxResult

PolicyMod = Msgbox("Is the Policy Mod # correct?", vbYesNo)

if PolicyMod = vbYes then
'Do execution for Yes
elseif PolicyMod = vbNo then
'Do stuff for No
end if
 
R

Rick Rothstein

The structure of what you want to do is this...

Response = MsgBox "Is the Policy Mod # correct?", vbYesNo
If Response = vbYes Then
' The user clicked the Yes button, so put your "Yes" code here
Else
' The user clicked the Yes button, so put your "No" code here
End If

Note that we must store the answer the user gives you in a variable (which I
called Response for this example) and then test the variable to see what it
is. VB defines some (many) constants that can be used for this purpose....
the vbYes constant contains the value that a Yes response from a Yes/No
MessageBox (note the 2nd argument on the MsgBox function call) returns and,
if you need to do the test in reverse, there is a vbNo constant the contains
the value a No response produces, so your If..Then test could be done
against that if need be.
 
R

Rick Rothstein

Damn! I wrote the first line incorrectly! It is a function call and needs
parentheses...

Response = MsgBox("Is the Policy Mod # correct?", vbYesNo)
If Response = vbYes Then
' The user clicked the Yes button, so put your "Yes" code here
Else
' The user clicked the Yes button, so put your "No" code here
End If
 
M

Munchkin

What's the code to stop running the macro? Sorry - I'm self taught at all
this, so I'm not up to par on codes as much as others here. I kind of am
learning as I go.
 
B

Barb Reinhardt

If you want to completely end execution, use END. If you want to exit the
current sub, use EXIT SUB.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top