Cancel userform event

Paella1

Active Member
Joined
Mar 10, 2005
Messages
382
I have a userform which displays on opening a file. It contains <u>Conditions of Use</u> to which the user must agree. If the user selects yes, the form unloads and the user can proceed. If the user selects No, the form unloads then the file closes without saving changes.

However, the user can still close the userform by clicking the close box at the top right, after which they could proceed but later claim they did not accept the <u>Conditions of Use</u>. I'd be left with no option but to have them put to death.

How do I ensure the file closes if the user clicks on the close button?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I have a userform which displays on opening a file. It contains Conditions of Use to which the user must agree. If the user selects yes, the form unloads and the user can proceed. If the user selects No, the form unloads then the file closes without saving changes.

However, the user can still close the userform by clicking the close box at the top right, after which they could proceed but later claim they did not accept the Conditions of Use. I'd be left with no option but to have them put to death.

How do I ensure the file closes if the user clicks on the close button?

Look up the QueryClose event
 
Upvote 0
Jaafar,
Both Queryclose and Terminate will fire all right. The problem is that they will fire <i>anytime</i> the form unloads. Which means the file then closes even if the user has clicked Yes.

My current line of thinking is that I need to pass variables between the userform's events, but surely there must be a simpler resolution thatn that.
 
Upvote 0
Maybe try using the CloseMode:
Rich (BB code):
Private Sub cmdRefuse_Click()
    UserForm_QueryClose False, vbFormControlMenu
End Sub
    
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    ThisWorkbook.Close False
End Sub
 
Upvote 0
A noble colleague just provided me with the following. It works.
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    
        'with compliments of Lord Schuckmeister
        If CloseMode = 0 Then Cancel = True
    
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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