Cancel button to cancel the whole macro

E

excelnut1954

I have UserForm1, that when completed, and the user clicks the OK
button, opens up UserForm2.
I also have a Cancel option in UserForm1.

Right now, if Cancel is clicked, the macro will still continue on to
UserForm2. That's because the only code I have in the Cancel sub is to
close UserForm1.

I want it to stop the whole macro when Cancel is selected.
What is the correct code I should enter in the Cancel sub to make the
whole macro stop?

Thanks,
J.O.
 
L

ljsmith

In the userform:

Private Sub cmdExit_Click()
Call exitMacro
End Sub

In the Main Module (for reference by both userforms if necessary...._

Public Sub exitMacro()
' quit the macro and exit Excel
Dim msg, style, title, response

If langChoice = "English" Then
msg = "If you exit the application, any unsaved data or
documents" _
& vbCrLf & " will be lost. Are you sure you wish to Exit?"
title = "Please confirm selected action."
style = vbYesNo + vbQuestion + vbDefaultButton1 +
vbApplicationModal
response = MsgBox(msg, style, title)
End If

If response = vbYes Then
exitNo = 0
Application.Quit
End
ElseIf response = vbNo Then
exitNo = 1
End If
End Sub


The message box is just a polite way of reminding the user, they'll
lose everything if they quit.
 
E

excelnut1954

I guess I didn't explain this well enough.
The user will click a command button in Sheet 1 to start the process.
This is in Private Sub CommandButton3_Click()

Within this Command sub, it directs the flow to UserForm 1, 2, then 3.
The Cancel button in question is in UserForm1. If the user clicks
Cancel within UserForm1, I want the macro to stop. I don't want to
exit Excel, but just to go back to Sheet 1, where the user started,
with no macro running.

Right now, I have it unload UserForm1 when Cancel is clicked. However,
it continues with the flow to UserForm2 & 3, etc now. That's why I
want code to stop the whole macro when the user clicks Cancel. I guess
I could also unload UserForm 2 & 3, but there is also code in the
Command sub after UserForm3 is unloaded.

I know my design may be crude, but I'm still learning all this, so at
this point, I'm doing whatever works. Just to make it clearer, here
is the layout:

******************************************
Private Sub CommandButton3_Click()
UserForm1 (this is where the cancel button is)
UserForm2
UserForm3

Code
Code
Code, etc.

End Sub
***********************************

Hope this makes it clearer.
Thanks for the help.
J.O.
 
L

ljsmith

excelnut1954 said:
I guess I didn't explain this well enough.
The user will click a command button in Sheet 1 to start the process.
This is in Private Sub CommandButton3_Click()

Within this Command sub, it directs the flow to UserForm 1, 2, then 3.
The Cancel button in question is in UserForm1. If the user clicks
Cancel within UserForm1, I want the macro to stop. I don't want to
exit Excel, but just to go back to Sheet 1, where the user started,
with no macro running.

******************************************
Private Sub CommandButton3_Click()
UserForm1 (this is where the cancel button is)
UserForm2
UserForm3

Code
Code
Code, etc.

End Sub
***********************************

Hope this makes it clearer.
Thanks for the help.
J.O.

******************************************
Private Sub CommandButton3_Click()
UserForm1 (this is where the cancel button is)
UserForm2
UserForm3

Code
Code
Code, etc.

Sheets("Sheet1").Select ' to select a specific worksheet - ensure the
info
' between the quotation marks is the
worksheet
' name as depicted in the Tab
Range("A1").Select ' if you want to focus on a specific cell in
the
' worksheet

End Sub
***********************************
 
E

excelnut1954

Thanks for your help, and time.
I pasted
Sheets("Sheet1").Select
in the Cancel sub. I thought that is where you meant it should go. (I
substituted the actual Sheet name in there)
But, when I ran the macro, and clicked Cancel, it kept on going onto
UserForm2, etc.
Isn't there something like a "cancel macro" command that I can insert
in there?
 

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