Userform exit macro

jpen

Active Member
Joined
Jun 19, 2002
Messages
401
Hi all,

I a macro (Main) I start a userform (FrmMenu.show).
This userform has a cancel button (CmdCancel). When the cancel button is activated I want the macro (Main) to stop running.

Any idea.

JH
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
The code used in the userform for the cancel button is:

Private Sub CmdCancel_Click()
Unload Me
End Sub

Now it goes back to the Main macro. And the Main macro it want to stop.
 
Upvote 0
Yes. I treid Exit Sub before and after the Unload Me.
But this doesn't work.
 
Upvote 0
You can use public variable for this.
Here's an example.
In a standard module (where "Main" code is if it's in one) put this at the very top of the
module:
Code:
Public Halted As Boolean
Then to modify your existing "Main" code you can do something like this.
Code:
Sub Main()
'At the beginning of "Main" macro code
Halted = 0

'Continue your "Main" macro code with what's supposed _
 to execute before the FrmMenu gets called.

'Just above the point at which you want the code _
 execution to stop if Cancel has been pressed. . .
If Halted <> 0 Then Exit Sub

'The remainder of your "Main" macro code

End Sub

Short of this helping then as texasalynn suggests, perhaps you can post your existing
code and we can help from there.

Hope it helps.
 
Upvote 0
If you want to stop all code use End.
 
Upvote 0
You can use public variable for this.
Here's an example.
In a standard module (where "Main" code is if it's in one) put this at the very top of the
module:
Code:
Public Halted As Boolean
Then to modify your existing "Main" code you can do something like this.
Code:
Sub Main()
'At the beginning of "Main" macro code
Halted = 0

'Continue your "Main" macro code with what's supposed _
 to execute before the FrmMenu gets called.

'Just above the point at which you want the code _
 execution to stop if Cancel has been pressed. . .
If Halted <> 0 Then Exit Sub

'The remainder of your "Main" macro code

End Sub

Short of this helping then as texasalynn suggests, perhaps you can post your existing
code and we can help from there.

Hope it helps.
Hi, all,

Dan, you forgot the userform part
Code:
Private Sub CmdCancel_Click() 
Halted = 1 'or True
Unload Me 
End Sub

or did I miss something ?

kind regards,
Erik
 
Upvote 0
Dan, you forgot the userform part
Code:
Private Sub CmdCancel_Click()
Halted = 1 'or True
Unload Me
End Sub

or did I miss something ?
Nope, you didn't miss anything. You're absolutely right.
I just plain forgot to include that part. :oops:
I guess it would be kind of important wouldn't it. :LOL:

Good thing you're keepin' an eye on me.
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,952
Members
448,535
Latest member
alrossman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top