Anglian,
I use the following code to accomplish what you want. The following will automatically close and save the workbook if inactive for 1 minute (you specify time). A userform will pop-up informing the user the sheet is about to close. A time counts down from twenty, then saves and closes the workbook.
In a module, place this code:
Option Explicit
Const sec = 1 / 60 / 60 / 24
Public BootTime As Date
Public Timer2Active As Boolean
Sub StartTimer()
' Start the Activity Timer
BootTime = Now + TimeValue("00:01:00")
' If the Activity Timer runs for the specified time
' call the "CloseBook" sub
Application.OnTime BootTime, "CloseBook"
End Sub
Sub CloseBook()
' At this point the main Timer has elapsed.
' Display and let the UserForm take charge.
Application.WindowState = xlMaximized
UserForm1.Show
End Sub
Sub ReallyCloseBook()
If Timer2Active = False Then Exit Sub
Unload UserForm1
'End
ThisWorkbook.Save
Application.Quit
ThisWorkbook.Close
End Sub
Private Sub count_down()
'Seconds on Userform1
Dim CD As Range
Dim BC As Range
Set CD = ThisWorkbook.Sheets("Sheet2").Range("A1")
Set BC = ThisWorkbook.Sheets("Sheet2").Range("B1")
UserForm1.Label3 = ThisWorkbook.Sheets("Sheet2").Range("B1").Value
CD = CD - sec
BC = BC - 1
If CD > 0 Then
Application.OnTime Now + sec, "count_down"
Else
End If
End Sub
PLACE THIS CODE IN WORKSHEET 1 CODE:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Check to see if the UserForm Timer is active.
' If it is, it has control, just exit this event without doing anything.
If Timer2Active = True Then Exit Sub
Application.OnTime BootTime, "CloseBook", , False
StartTimer
End Sub
PLACE THIS CODE IN YOUR WORKBOOK OPEN CODE:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.OnTime earliesttime:=BootTime, _
procedure:="CloseBook", schedule:=False
End Sub
Private Sub Workbook_Open()
StartTimer
End Sub
CREATE A USERFORM1 AND PLACE THIS CODE IN IT:
Private Sub UserForm_Activate()
ThisWorkbook.Sheets("Sheet2").Range("A1") = "00:00:20"
ThisWorkbook.Sheets("Sheet2").Range("B1") = 20
count_down
' Set the Timer2Active Flag to True.
' This stops the SheetSelectionFlag from processing it's code
' This UserForm now has control over what will be done next.
Timer2Active = True
' Start a timer within this UserForm. If the allotted time elapses
' without pressing the CommandButton, call "ReallyCloseBook"
Application.OnTime Now + TimeValue("00:00:20"), "ReallyCloseBook"
End Sub
Private Sub count_down()
'Seconds on Userform1
Dim CD As Range
Dim BC As Range
Set CD = ThisWorkbook.Sheets("Sheet2").Range("A1")
Set BC = ThisWorkbook.Sheets("Sheet2").Range("B1")
UserForm1.Label3 = ThisWorkbook.Sheets("Sheet2").Range("B1").Value
CD = CD - sec
BC = BC - 1
If CD > 0 Then
Application.OnTime Now + sec, "count_down"
Else
End If
End Sub
CREATE A BUTTON ON THAT USERFORM1 AND PLACE THIS CODE:
Private Sub CommandButton1_Click()
' Set the Timer2Active flag to False in order that the
' SheetSelectionChange Event can monitor activity.
Timer2Active = False
' Restart the Main timer again
StartTimer
' Dispense with the UserForm
Unload UserForm1
End
End Sub
YOU WILL ALSO NEED A LABEL ON THE USERFORM CALLED LABEL3
This is what shows the count down when the sheet is inactive.
Hope this helps
