ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Cancel button to cancel the whole macro (https://www.excelbanter.com/excel-programming/350576-cancel-button-cancel-whole-macro.html)

excelnut1954

Cancel button to cancel the whole macro
 
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.


Bob Phillips[_6_]

Cancel button to cancel the whole macro
 
Why not just Unload the other from as well?

--
HTH

RP
"excelnut1954" wrote in message
ups.com...
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.




ljsmith[_2_]

Cancel button to cancel the whole macro
 

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.


--
ljsmith


------------------------------------------------------------------------
ljsmith's Profile: http://www.excelforum.com/member.php...o&userid=30531
View this thread: http://www.excelforum.com/showthread...hreadid=501832


excelnut1954

Cancel button to cancel the whole macro
 
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?



All times are GMT +1. The time now is 06:11 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com