[英]How to automatically Save and Exit out of a worksheet after a set amount of time? (Excel VBA)
如果超过5分钟没有任何活动,是否可以使打开的工作表自行关闭?
因此,例如:我在工作表上工作了一段时间,然后在打开工作表的情况下走了20分钟。 网络上的某人需要访问工作表,但不能访问,因为我在工作表上。
我想要这样,以便在我离开办公桌超过5分钟后,床单会自行保存并关闭床单。
这可能吗? 如果可以,怎么办? 我可以找到脚本来显示如何保存和关闭工作表,但是我还没有找到使用计时器的脚本...
这是来自链接的信息,因此该问题可用作参考:
将此代码作为模块插入:
' DateTime : 09/05/2007 08:43
' Author : Roy Cox (royUK)
' Website : Clck here for more examples and Excel Consulting
' Purpose : Place in a standard module
' Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.
'---------------------------------------------------------------------------------------
Option Explicit
Public EndTime
Sub RunTime()
Application.OnTime _
EarliestTime:=EndTime, _
Procedure:="CloseWB", _
Schedule:=True
End Sub
Sub CloseWB()
Application.DisplayAlerts = False
With ThisWorkbook
.Save
.Saved = True
.Close
End With
End Sub
将此插入“ ThisWorkbook”
Private Sub Workbook_Open()
'--> Set Time Below
EndTime = Now + TimeValue("00:00:00")
RunTime
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If EndTime Then
Application.OnTime _
EarliestTime:=EndTime, _
Procedure:="CloseWB", _
Schedule:=False
EndTime = Empty
End If
'--> Set Time Below
EndTime = Now + TimeValue("00:00:00")
RunTime
End Sub
好的,下面是原始答案,经过更多研究,我提出了自己的答案。
打开开发人员的部分后,您将找到工作表,将下面的代码放入ThisWorkbook中。 这将使您的代码能够遍历整个工作表。 现在,我在其中设置了一个10:00分钟的初始计时器,如果事实发生后有活动,则在05:00分钟的计时器处进行设置。 您可以将其更改为任何您想要的。
Option Explicit
Private Sub Workbook_Open()
EndTime = Now + TimeValue("00:10:00")
RunTime
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If EndTime Then
Application.OnTime _
EarliestTime:=EndTime, _
Procedure:="CloseWB", _
Schedule:=False
EndTime = Empty
End If
EndTime = Now + TimeValue("00:05:00")
RunTime
End Sub
这下面的部分需要进入一个新创建的模块,将其命名为您想要的名称,我的名为SaveWB
Option Explicit
Public EndTime
Sub RunTime()
Application.OnTime _
EarliestTime:=EndTime, _
Procedure:="CloseWB", _
Schedule:=True
End Sub
Sub CloseWB()
Application.DisplayAlerts = False
With ThisWorkbook
ThisWorkbook.Close savechanges:=True
End With
End Sub
我将代码从:
With ThisWorkbook
.Save
.Saved = True
.Close
End With
到上面的东西。
With ThisWorkbook
ThisWorkbook.Close savechanges:=True
End With
我创建的零件起作用,而最初发布的零件在关闭时起作用,但不保存。 随心所欲地进行操作,并根据自己的喜好进行更改,但我很高兴我能使其正常运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.