Workbook_BeforeClose(Cancel As Boolean)

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,604
Office Version
  1. 365
Platform
  1. Windows
Hi all

I have a Userform that on selecting YES a certain macro is run on selecting NO the form is closed and the workbook closed...or at least that is what I want..

code attached to the NO button on the userform
Code:
Private Sub CommandButton2_Click()
Unload Me
ActiveWorkbook.Close
End Sub
however there is also this code in the Workbook_BeforeClose(Cancel As Boolean) section

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Dim myForm As Userclose
    
    If Time > TimeValue("18:00:00") Then
        Cancel = True
        Set myForm = Userclose
        myForm.Show
    End If
    
End Sub

so an error occurs because as I understand it when the NO button is pressed the userform is closed but then the private Sub code above kicks is

How can I over ride this on close event so that when the NO button is selected the userform is closed and the workbook is closed too without running the Private Sub Workbook_BeforeClose(Cancel As Boolean) macro

hope this makes sense and thanks in advance
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Rich (BB code):
Private Sub CommandButton2_Click()
Unload Me
Application.EnableEvents = False
ActiveWorkbook.Close
End Sub

lenze
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,916
Members
452,949
Latest member
beartooth91

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