新建一个模块,在模块当中输入以下代码
Public Runtime, Lasttime
Sub 计时器()
If Now >= Lasttime Then
ThisWorkbook.Save
If Application.Windows.Count = 1 Then
Application.Quit
Else
ThisWorkbook.Close True
End If
Exit Sub
End If
Runtime = Lasttime
Application.OnTime Runtime, "计时器"
End Sub
打开VBA工程的Thisworkbook,在Thisworkbook当中输入以下代码
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.OnTime EarliestTime:=TimeValue(Runtime), _
Procedure:="计时器", Schedule:=False
On Error GoTo 0
End Sub
Private Sub Workbook_Open()
Lasttime = Now + TimeValue("00:02:00")
Call 计时器
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Lasttime = Now + TimeValue("00:02:00") '此处修改定时时间。
End Sub
代码粘贴完之后,保存关闭工作簿即可,下次打开之后,2分钟之内如果没有选择单元格操作,文件自动保存关闭!