仅在打开时保存工作簿的条件

发布于 2025-02-13 22:25:41 字数 359 浏览 0 评论 0原文

嗨,我所有的代码可以自动完成一个工作簿

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

差↓一点笑了 2025-02-20 22:25:41

您的问题是您永远不会停止计时器 - 即使您关闭了工作簿,它也会保持活跃。分钟结束后,VBA想要调用当前尚不可用的sub(save1)(由于工作簿关闭),因此VBA要求Excel打开文件,以便它可以执行例程。

如果您以某种方式添加了工作簿,则无济于事,因为当时,它 已经再次打开。

您需要做的是关闭工作簿时停止计时器。该事件是workbook_beforeclose。现在停止在VBA中停止计时器有点棘手:您再次调用application.intime -Method,您必须提供当时设置时发布时发出的确切参数:例程的名称需要匹配的时间。唯一的区别是,您将第四参数(Schedule)设置为false。

结果,您的代码需要跟踪在最后一次呼叫ontime的时间上提供的时间。查看以下代码:

在工作簿模块中:

Private Sub Workbook_Open()
    StartTimer
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    StopTimer
End Sub

在常规代码模块中:

Option Explicit

Dim FireTime As Variant     ' This holds the time when Ontime was called the last time

Sub StartTimer()
    FireTime = Now + TimeValue("00:01:00")
    Application.OnTime FireTime, "Save1"
    ' ThisWorkbook.Sheets(1).Cells(1, 1) = FireTime
End Sub

Sub StopTimer()
    If Not IsEmpty(FireTime) Then
        Application.OnTime FireTime, "Save1", , Schedule:=False
        FireTime = Empty
    End If
End Sub

Sub Save1()
    Debug.Print "tick"
    ' put your actions here, eg saving 
    StartTimer  ' Schedule the next Timer event
End Sub

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 the Application.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:

Private Sub Workbook_Open()
    StartTimer
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    StopTimer
End Sub

In a regular code module:

Option Explicit

Dim FireTime As Variant     ' This holds the time when Ontime was called the last time

Sub StartTimer()
    FireTime = Now + TimeValue("00:01:00")
    Application.OnTime FireTime, "Save1"
    ' ThisWorkbook.Sheets(1).Cells(1, 1) = FireTime
End Sub

Sub StopTimer()
    If Not IsEmpty(FireTime) Then
        Application.OnTime FireTime, "Save1", , Schedule:=False
        FireTime = Empty
    End If
End Sub

Sub Save1()
    Debug.Print "tick"
    ' put your actions here, eg saving 
    StartTimer  ' Schedule the next Timer event
End Sub
哆啦不做梦 2025-02-20 22:25:41

您可以使用:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnTime Now + TimeValue("00:01:00"), "Save1",, False
End Sub

You can use:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnTime Now + TimeValue("00:01:00"), "Save1",, False
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文