仅在打开时保存工作簿的条件
嗨,我所有的代码可以自动完成一个工作簿
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:01:00"), "Save1"
End Sub
Sub Save1()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.OnTime Now + TimeValue("00:01:00"), "Save1"
End Sub
问题,现在它可以不断开放以保存。任何人都知道该语法是什么才能在打开时才保存的?
Hi all I have this code to autosave a workbook
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:01:00"), "Save1"
End Sub
Sub Save1()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.OnTime Now + TimeValue("00:01:00"), "Save1"
End Sub
Problem is now it keeps opening to save. Anyone know what the syntax is to let it save only when its open?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是您永远不会停止计时器 - 即使您关闭了工作簿,它也会保持活跃。分钟结束后,VBA想要调用当前尚不可用的sub(
save1
)(由于工作簿关闭),因此VBA要求Excel打开文件,以便它可以执行例程。如果您以某种方式添加了工作簿,则无济于事,因为当时,它 已经再次打开。
您需要做的是关闭工作簿时停止计时器。该事件是
workbook_beforeclose
。现在停止在VBA中停止计时器有点棘手:您再次调用application.intime
-Method,您必须提供当时设置时发布时发出的确切参数:例程和的名称需要匹配的时间。唯一的区别是,您将第四参数(Schedule
)设置为false。结果,您的代码需要跟踪在最后一次呼叫
ontime
的时间上提供的时间。查看以下代码:在工作簿模块中:
在常规代码模块中:
Your problem is that you never stop the timer - it stays active even if you close the workbook. When the minute is over, VBA want to call a Sub (
Save1
) that is currently not available (as the workbook is closed), so VBA asks Excel to open the file so that it can execute the routine.It will not help you if you would somehow add a check if the workbook is open because at that time, it is already open again.
What you need to do is to stop your timer when you close the workbook. The event for that is
Workbook_BeforeClose
. Now stopping a timer in VBA is a little bit tricky: You call theApplication.OnTime
-method again, and you have to provide the exact parameter that you issued when the timer was set the last time: The name of the routine and the time need to match. The only difference is that you set the 4th parameter (Schedule
) to False.As a consequence, your code need to keep track what time was provided at the last call to
OnTime
. Have a look to the following code:In the Workbook-Module:
In a regular code module:
您可以使用:
You can use: