PDA

View Full Version : Solved: CLOSE WORBOOK AT SPECIFIED TIME



BENSON
01-21-2007, 11:19 PM
HI all , is it possible to write a VBA code that would not allow the user to close a workbook before a specified time.For example a person opens a workbook when arriving at the office.He /She would be unable to close the worbook before 4:30 pm the same day .The purpose of the excercise would be to force the user to remain in the office untill that time.

MANY Thanks

johnske
01-22-2007, 01:06 AM
I don't think that'll stop them leaving :)

In the ThisWorkbook code module...

Option Explicit
'
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not TimeValue(Time) >= "4:30:00 PM" Then Cancel = True
End Sub

BENSON
01-22-2007, 01:27 AM
That works great johnske many thanks. I would also like to insert a Msg Box to inform them why the workbook wont close could you help me get started .

Thaks

johnske
01-22-2007, 01:36 AM
Option Explicit
'
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not TimeValue(Time) >= "4:30:00 PM" Then
MsgBox "?It's only " & TimeValue(Time) & "? - Get back to work you slacker!!"
Cancel = True
End If
End Sub

BENSON
01-22-2007, 02:00 AM
MANY THANKS I Like the Humour

BENSON
01-22-2007, 07:37 AM
I Might have been too eager to mark this one solved the time has elasped but the code will not allow the user to close the workbook any suggestions I have copied code below

Option Explicit
'
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not TimeValue(Time) >= "4:30:00 PM" Then
MsgBox "?It's only " & TimeValue(Time) & "? - Get back to work you slacker!!"
Cancel = True
End If
End Sub

johnske
01-22-2007, 08:00 AM
Works for me, remember that it kicks in again at midnight tho...