Disable close actions if time > 1800

redspanna

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

I want certain events to happen if the user tries to close my "records" workbook after 1800.

So if the X button is clicked or File>Close, File>Exit options are chosen and the time is after 1800 then display the "error" userform.
If the time is before 1800 then allow closure as normal.

also..on workbook open....

If the time is before 0700 and cell A1 on sheet1 is blank then show userform "error2".
If the time is after 0700 and A1 isn't blank then open as normal

NB: if call A1 isn't blank its populated with the date format yyyy-mm-dd

would really appreciate any code to help carry out the above.

many thanks in advance
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
For the workbook close requirement, put something like this in the ThisWorkbook module (change UserForm1 to the name of your userform):
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Dim myForm As UserForm1
    
    If Time > TimeValue("18:00:00") Then
        Cancel = True
        Set myForm = UserForm1
        myForm.Show
    End If
    
End Sub
Use similar code in the Workbook_Open event to handle the workbook open requirement.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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