以下代码根据单元格值进行自动保存。
Sub AutoSaveIt()
If Sheets("BOH General").Range("A102").Value > 0 Then
ThisWorkbook.Save
Application.OnTime Now + TimeSerial(0, Sheets("BOH General").Range("A102").Value, 0), "AutoSaveIt"
End If
End Sub
我接近什么代码打电话来打破这个循环?
编辑:我试图避免重新打开工作簿并在宏关闭后再次运行宏
答案 0 :(得分:1)
barrowc的链接给了我一篇文章所需的搜索词 here
我想出了一些似乎有效的代码 - 如果有人需要的话,这里有一个缩写版本:
Public runwhen As Variant
Sub AutoSaveTimer()
If Sheets("BOH General").Range("A102").Value > 0 Then
RunWhen = Now + TimeSerial(0, Sheets("BOH General").Range("A102").Value, 0)
Application.OnTime EarliestTime:=RunWhen, Procedure:="AutoSaveIt", _
Schedule:=True
Else
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, Procedure:="AutoSaveIt", _
Schedule:=False
On Error GoTo 0
End If
End Sub
Sub AutoSaveIt()
ThisWorkbook.Save
Call AutoSaveTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Cancel autosave
Sheets("BOH General").Range("A102").Value = 0
call AutoSaveTimer
End Sub
答案 1 :(得分:0)
您是否可以使用BeforeClose事件将A102值更改为零?
也许将它与Workbook_Open事件配对以将其更改回来。
编辑:
Sub AutoSaveIt()
If ThisWorkbook.Sheets(1).Range("A1").Value > 0 Then
ThisWorkbook.Save
Application.OnTime Now + TimeSerial(0, ThisWorkbook.Sheets(1).Range("A1").Value, 0), "AutoSaveIt"
Debug.Print "saved at " & Now()
End If
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Sheets(1).Range("A1").Value = 0
ThisWorkbook.Save
Application.Quit
End Sub
Private Sub Workbook_Open()
ThisWorkbook.Sheets(1).Range("A1").Value = 1
Application.OnTime Now + TimeSerial(0, 0, ThisWorkbook.Sheets(1).Range("A1").Value), "AutoSaveIt"
End Sub