Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 9
Default How to get userform Cancel button to invoke Exit Sub in calling macro?

I have a userform that is called from inside a sub in a module of a workbook. In order to keep most of the code inside the module, the userform only sets variables in the sub. Here is the code in the sub:

'create and initialize variables
Dim Bkp,XSub as Boolean
Dim BkpSfx as String

Bkp = True
XSub = False

'call options box
BackupOptionsBox.Show

'Set main subroutine variables
Bkp = BackupOptionsBox.YesOption
BkpSfx = "_" & BackupOptionsBox.SfxBox.Value
XSub = BackupOptionsBox.CancelButton.Value

'unload backup box
Unload BackupOptionsBox
Debug.Print ("Yes=" & Bkp & ", No=" & _ BackupOptionsBox.NoOption & ", XSub=" & XSub)
'Drop out of sub if cancel button was pushed
If XSub Then
prj.FileOpenEx Name:=PrjName & PrjXtn
Exit Sub
End If

The box is has the following controls:
1. Two radio buttons, YesOption and NoOption, which toggle YesOption.Value
2. A text input field, SfxBox, for typing which is enabled when YesOption = True
3. An OK button
4. A Cancel button

I have the following event sub for CancelButton:

Private Sub CancelButton_Click()
CancelButton.Value = True
Me.Hide
End Sub

When I run the main subroutine and click the OK button, the code executes correctly (Bkp = True or False depending on the option button I selected, and XSub = False in the debug.print statement).

However, when I just click the Cancel button, XSub remains false in the debug.print statement.

Any ideas?

EggHeadCafe - Software Developer Portal of Choice
ASP Intrinsics Under COM Plus in the VB Windows 2000 ComponentASP
http://www.eggheadcafe.com/tutorials...under-com.aspx
  #2   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 35,218
Default How to get userform Cancel button to invoke Exit Sub in callingmacro?

You could add another boolean(?) variable (either public in a General module) or
a variable in the userform module that you can test. Then change that variable
in the cancelbutton_click to false and in the okbutton_click to True.

Another option would be to use the userform's tag as that variable.

Private Sub CancelButton_Click()
me.tag = False
Me.Hide
End Sub


And make it true in okbutton_click.

Then check that tag in the general procedure.

if BackupOptionsBox.tag = true then
'ok code
else
'cancel code
end if



Craig, Remillard wrote:

I have a userform that is called from inside a sub in a module of a workbook. In order to keep most of the code inside the module, the userform only sets variables in the sub. Here is the code in the sub:

'create and initialize variables
Dim Bkp,XSub as Boolean
Dim BkpSfx as String

Bkp = True
XSub = False

'call options box
BackupOptionsBox.Show

'Set main subroutine variables
Bkp = BackupOptionsBox.YesOption
BkpSfx = "_" & BackupOptionsBox.SfxBox.Value
XSub = BackupOptionsBox.CancelButton.Value

'unload backup box
Unload BackupOptionsBox
Debug.Print ("Yes=" & Bkp & ", No=" & _ BackupOptionsBox.NoOption & ", XSub=" & XSub)
'Drop out of sub if cancel button was pushed
If XSub Then
prj.FileOpenEx Name:=PrjName & PrjXtn
Exit Sub
End If

The box is has the following controls:
1. Two radio buttons, YesOption and NoOption, which toggle YesOption.Value
2. A text input field, SfxBox, for typing which is enabled when YesOption = True
3. An OK button
4. A Cancel button

I have the following event sub for CancelButton:

Private Sub CancelButton_Click()
CancelButton.Value = True
Me.Hide
End Sub

When I run the main subroutine and click the OK button, the code executes correctly (Bkp = True or False depending on the option button I selected, and XSub = False in the debug.print statement).

However, when I just click the Cancel button, XSub remains false in the debug.print statement.

Any ideas?

EggHeadCafe - Software Developer Portal of Choice
ASP Intrinsics Under COM Plus in the VB Windows 2000 ComponentASP
http://www.eggheadcafe.com/tutorials...under-com.aspx


--

Dave Peterson
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
Cancel/Exit Sub Howard Excel Discussion (Misc queries) 3 December 16th 08 09:53 PM
how to create a macro button to exit a program PACEBIT0708 Excel Worksheet Functions 1 April 23rd 08 04:43 PM
how do i make a button from a userform exit onto the document in . Pieface New Users to Excel 1 April 17th 07 07:35 PM
Disable Exit button on a UserForm Noemi Excel Discussion (Misc queries) 1 September 14th 06 09:37 PM
How do i create a command button or macro to exit Microsoft Excel raj Excel Worksheet Functions 1 April 30th 05 02:17 PM


All times are GMT +1. The time now is 01:52 AM.

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

About Us

"It's about Microsoft Excel"