Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Aborting a Sub procedure from a UserForm


I have a Sub procedure that invokes a userform. On the userform there
are a series of radio buttons and two command buttons, one for OK and
one for CANCEL. When the CANCEL button is pressed, I want the userform
to either hide or unload itself -- I know how to do this part -- and I
want the Sub procedure to abort.

How do I do this last part? I can't figure out how to use the CANCEL
button as input to the Sub procedure to get it to abort itself.


--
shellshock
------------------------------------------------------------------------
shellshock's Profile: http://www.excelforum.com/member.php...o&userid=24935
View this thread: http://www.excelforum.com/showthread...hreadid=391599

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Aborting a Sub procedure from a UserForm

I declare a Public Boolean variable in the Declaration section of the
module containing the Sub Procedure eg Public binAbortSub as Boolean.
In the Sub make it equal False before showing the UserForm (binAbortSub
= False). In the code section of the Cancel button type
BinAbortSub = True
Unload Userform (Use your UserForm's Name here)
In the Sub, straight after the line with UserForm.Show, type
If binAbortSub = True then
exit Sub
End If
This works for me but I'm no expert. There might be a better way.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Aborting a Sub procedure from a UserForm


Good morning shellshock

After the uloading (or hiding) the userfrom use this command to abort
the procedu

Exit Sub

HTH

DominicB


--
dominicb
------------------------------------------------------------------------
dominicb's Profile: http://www.excelforum.com/member.php...o&userid=18932
View this thread: http://www.excelforum.com/showthread...hreadid=391599

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Aborting a Sub procedure from a UserForm


Thanks to both of you for your suggestions. :) In the end, I used an
integer testflag very much like the boolean variable as suggested by
hanjohn, and I also used the Exit Sub command as suggested by
dominicb.

I inserted the testflag (designated X) in my userform code, which feeds
back to the main procedure as an indicator of whether or not the main
procedure should continue. Here's the userform code:

Option Explicit
Public RCType As Worksheet
Public ws As Worksheet 'this is a placeholder
Public X As Integer 'this is the testflag

Private Sub OptionButton1_Click() 'sample radio button code
Set ws = Worksheets("Sheet1")
X = 1
End Sub

Private Sub OK_Click()
Set RCType = ws
Hide
End Sub

Private Sub CANCEL_Click()
Unload Me
End Sub

The procedure that calls the code:

Sub main_procedure()
UserForm1.Show

If UserForm1.X = 1 Then
UserForm1.RCType.Activate
etc.etc.etc.
Else
Exit Sub
End If

End Sub

So, when the user selects a radio button, ws is defined, and X=1. Then,
when the user presses OK, RCType is equated to ws and the rest of
main_procedure runs.

If the user presses the OK button without first selecting a radio
button, X is still equal to 0, so we Exit Sub. If the user presses
CANCEL, the userform is unloaded, which I guess means that X=0, so that
again we Exit Sub.


--
shellshock
------------------------------------------------------------------------
shellshock's Profile: http://www.excelforum.com/member.php...o&userid=24935
View this thread: http://www.excelforum.com/showthread...hreadid=391599

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 85
Default Aborting a Sub procedure from a UserForm

Shell,
Nothing wrong with your solution. The approach I use is to set the
Userform.Tag parameter, or one of the OptionButton.Tag paramters during a
cancel operation, then check it from the calling sub.
AlexJ

"shellshock" wrote
in message ...

Thanks to both of you for your suggestions. :) In the end, I used an
integer testflag very much like the boolean variable as suggested by
hanjohn, and I also used the Exit Sub command as suggested by
dominicb.

I inserted the testflag (designated X) in my userform code, which feeds
back to the main procedure as an indicator of whether or not the main
procedure should continue. Here's the userform code:

Option Explicit
Public RCType As Worksheet
Public ws As Worksheet 'this is a placeholder
Public X As Integer 'this is the testflag

Private Sub OptionButton1_Click() 'sample radio button code
Set ws = Worksheets("Sheet1")
X = 1
End Sub

Private Sub OK_Click()
Set RCType = ws
Hide
End Sub

Private Sub CANCEL_Click()
Unload Me
End Sub

The procedure that calls the code:

Sub main_procedure()
UserForm1.Show

If UserForm1.X = 1 Then
UserForm1.RCType.Activate
etc.etc.etc.
Else
Exit Sub
End If

End Sub

So, when the user selects a radio button, ws is defined, and X=1. Then,
when the user presses OK, RCType is equated to ws and the rest of
main_procedure runs.

If the user presses the OK button without first selecting a radio
button, X is still equal to 0, so we Exit Sub. If the user presses
CANCEL, the userform is unloaded, which I guess means that X=0, so that
again we Exit Sub.


--
shellshock
------------------------------------------------------------------------
shellshock's Profile:

http://www.excelforum.com/member.php...o&userid=24935
View this thread: http://www.excelforum.com/showthread...hreadid=391599



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
userform output to procedure shellshock Excel Programming 2 July 29th 05 11:31 PM
Looping procedure calls userform; how to exit loop (via userform button)? KR Excel Programming 6 July 27th 05 12:57 PM
Quit UserForm and Procedure Maria[_7_] Excel Programming 6 September 5th 04 11:29 PM
VBA question - calling Procedure from userform ajliaks[_3_] Excel Programming 2 April 14th 04 09:11 PM
Passing Userform as an argument to a procedure Howard Kaikow Excel Programming 12 October 24th 03 03:24 PM


All times are GMT +1. The time now is 10:17 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"