Exiting a routine when clicking cancel on a userform

Pauljj

Well-known Member
Joined
Mar 28, 2004
Messages
2,047
Dear all, I have tried looking at existing posts but so far nothing works

I have a user form that has an OK and Cancel button. When I click on cancel, the form unloads but still moves onto the remaining part of the code. I have tried the following without success

In my main module

Code:
Public cancflg as boolean

--------------------------------------------------------------
Sub cancelflag(cancl as boolean)
canclflg = cancel
End sub
--------------------------------------------------------------

In the userform, when clicking Cancel

Code:
Private Sub CommandButton1_Click

Call cancelflag(true)
Unload me
End sub

In addition to this

Code:
Private Sub Userform_Initialize()

Call Cancelflag(False)

End Sub


In my main code back in the module

Code:
FrmInput.show

If Cancelflag = True then

Exit sub

End if


Is there something I have missed ?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi,
quick glance & note you have a typo in both parameter & variable names

Rich (BB code):
Public cancflg As Boolean


Sub cancelflag(cancel As Boolean)
cancflg = cancel
End Sub


and need to test Public boolean variable

VBA Code:
frmInput.Show

If cancflg = True Then Exit Sub

'rest code

Dave
 
Upvote 0
Your form should not unload itself. It should just hide. You can then test its cancflag member before unloading it, and exiting if necessary.
 
Upvote 0
Thank you both for your replies. I have made the changes to the typos, thanks dmt32. When I step through the code, this is what happens

1) On Initialise CANCFLG is set to FALSE
2) This calls the Cancelflag routine
3) Form appears
4) CANCFLG now appears as <Out of Context>
5) I click cancel
6) CANCFLG now appears as FALSE
7) This calls the Cancelflag routine
8) CANCFLG now appears as TRUE
9) Upon retuning to the cancel click routine CANCFLG has returned to <Out of Context>
 
Upvote 0
Hi,
if you just want your buttons to respond in similar way to a msgbox then you could just use the msgbox enumeration

example

code in your form

VBA Code:
Private Sub CommandButton1_Click()
    Me.Hide
    Me.Tag = vbCancel
End Sub

Private Sub CommandButton2_Click()
    Me.Hide
    Me.Tag = vbOK
End Sub

Function GetResponse() As VbMsgBoxResult
    Me.Show
    GetResponse = Val(Me.Tag)
    Unload Me
End Function

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    'prevent close by X
    Cancel = CloseMode = 0
End Sub

code in your standard module

VBA Code:
Sub Pauljj()

    Dim Response As VbMsgBoxResult

    Response = frmInput.GetResponse
    
    'cancel pressed
    If Response = vbCancel Then Exit Sub
    
    'rest of code
    MsgBox "Hello"
    

End Sub

You can also create your own custom Enumeration if preferred

Hope Helpful

Dave
 
Upvote 0
Solution
Hi,
if you just want your buttons to respond in similar way to a msgbox then you could just use the msgbox enumeration

example

code in your form

VBA Code:
Private Sub CommandButton1_Click()
    Me.Hide
    Me.Tag = vbCancel
End Sub

Private Sub CommandButton2_Click()
    Me.Hide
    Me.Tag = vbOK
End Sub

Function GetResponse() As VbMsgBoxResult
    Me.Show
    GetResponse = Val(Me.Tag)
    Unload Me
End Function

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    'prevent close by X
    Cancel = CloseMode = 0
End Sub

code in your standard module

VBA Code:
Sub Pauljj()

    Dim Response As VbMsgBoxResult

    Response = frmInput.GetResponse
   
    'cancel pressed
    If Response = vbCancel Then Exit Sub
   
    'rest of code
    MsgBox "Hello"
   

End Sub

You can also create your own custom Enumeration if preferred

Hope Helpful

Dave
Thank you for this Dave, I will give this a try. One question I have, I do have other code being carried out if the user clicks OK, would this still work ? I guess it's best I just try
 
Upvote 0
Thank you for this Dave, I will give this a try. One question I have, I do have other code being carried out if the user clicks OK, would this still work ? I guess it's best I just try

Hi,
suggested idea acts in same way as the msgbox function in that it just returns an integer value based on which button is pressed.
In my example, Cancel button will return 2 (vbCancel) and OK button will return 1 (vbOK)

Your other code for an OK response would be placed where "rest of code" is shown

VBA Code:
 Response = frmInput.GetResponse()
    
    'cancel pressed
    If Response = vbCancel Then
    
        Exit Sub
        
    Else

    'OK pressed
    'rest of code
    MsgBox "Hello"
    
    
    End If

Dave
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,765
Members
449,049
Latest member
greyangel23

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