简体   繁体   English

Application.OnTime 在其他工作簿打开时重新打开工作簿

[英]Application.OnTime reopens workbook when other workbook is open

I have a timer that closes my workbook after 5 minutes.我有一个计时器,可以在 5 分钟后关闭我的工作簿。 The issue is when i have another workbook open the workbook with the timer will reopen when i try to close it.问题是当我打开另一个工作簿时,当我尝试关闭它时,带有计时器的工作簿将重新打开。

Earlier i had the countdown to "tick" every second but that messed up the view of comments making them blink for every countdown tick.早些时候,我每秒钟都有一个“滴答”的倒计时,但这弄乱了评论的视图,使它们在每个倒计时滴答时闪烁。 When i had that I didn't see any issues with reopening of the workbook.当我拥有它时,我没有看到重新打开工作簿有任何问题。

I have this in both my module and thisworkbook我的模块和本工作簿中都有这个

Public gCount as Date

These two codes are in my module.这两个代码在我的模块中。 The timer is displayed in a cell (Worksheets("kode").Range("H3")) and counts down every 10 seconds计时器显示在一个单元格中 (Worksheets("kode").Range("H3")) 并每 10 秒倒计时

Sub Timer()
gCount = Now + TimeValue("00:00:10")
Application.OnTime gCount, "ResetTime"
End Sub

Sub ResetTime()
Dim xRng As Range
If ThisWorkbook.Worksheets("kode").Range("H3") = "" Then GoTo Endsub
Set xRng = Application.ThisWorkbook.Worksheets("kode").Range("H3")
xRng.Value = xRng.Value - TimeSerial(0, 0, 10)
If xRng.Value <= 1.15740740740741E-05 Then
Call SavedAndClose
Exit Sub
End If
Call Timer

Endsub:

End Sub

This code is in ThisWorkbook此代码在 ThisWorkbook 中

Private Sub Workbook_BeforeClose(Cancel As Boolean)

On Error Resume Next

gCount = Now + TimeValue("00:00:10")
Application.OnTime gCount, "ResetTime", Schedule:=False

ThisWorkbook.Worksheets("Interface").Select

'Hides all sheets but the interface sheet
Sheet2.Visible = False
Sheet3.Visible = False
Sheet6.Visible = False
Sheet7.Visible = False
Sheet8.Visible = False

End Sub

There too is a place where the cell Worksheets("kode").Range("H3") is set to 00:05:01 and a Workbook_SheetSelectionChange where it resets it to 00:05:01也有一个地方,其中单元格 Worksheets("kode").Range("H3") 设置为 00:05:01 和一个 Workbook_SheetSelectionChange 将其重置为 00:05:01

The sheet closes when Worksheets("kode").Range("H3") is at 00:00:01当 Worksheets("kode").Range("H3") 在 00:00:01 时,工作表关闭

If i remove the "On Error Resume Next" the code makes a 1004 run-time error when i try to close the workbook.如果我删除“On Error Resume Next”,当我尝试关闭工作簿时,代码会产生 1004 运行时错误。

Hope that someone can help me close my workbook希望有人能帮我关闭我的工作簿

Best regards此致

If i remove the "On Error Resume Next" the code makes a 1004 run-time error when i try to close the workbook.如果我删除“On Error Resume Next”,当我尝试关闭工作簿时,代码会产生 1004 运行时错误。

And that is why you should not put On Error Resume Next everywhere to silence errors instead of fixing them.这就是为什么你不应该On Error Resume Next任何地方放置On Error Resume Next来消除错误而不是修复它们。

Application.OnTime can schedule the same procedure multiple times for different times of day. Application.OnTime可以在一天中的不同时间多次安排相同的程序。 For this reason, it can only unschedule a previously scheduled entry when you provide the exact time for which it was scheduled - if you provide a time for which there is no scheduled entry, you will get a runtime error 1004.因此,它只能在您提供计划的确切时间时取消计划先前计划的条目 - 如果您提供没有计划条目的时间,您将收到运行时错误 1004。

Now + TimeValue("00:00:10") returns a different value each time you call it. Now + TimeValue("00:00:10")每次调用时都会返回不同的值。

If you want to be able to cancel a previously set entry, store the time in a module-level variable and use that variable for both scheduling and unscheduling.如果您希望能够取消先前设置的条目,请将时间存储在模块级变量中,并将该变量用于调度和取消调度。 Your module-level gCount variable would do, but:您的模块级gCount变量可以,但是:

  • You have two of them ("I have this in both my module and thisworkbook")你有两个(“我的模块和本工作簿中都有这个”)
  • You overwrite the previously stored value with a useless new one right before calling Schedule:=False .在调用Schedule:=False之前,您用一个无用的新值覆盖先前存储的值。

Make sure you only have one gCount , and only assign to it before scheduling a call, not before unscheduling it.确保您只有一个gCount ,并且只在安排呼叫之前分配给它,而不是在gCount安排之前分配给它。

I found an answer to my own comment to GSergs answer:我找到了我自己对 GSergs 的评论的答案:

I made a Msgbox with vbYesNoCancel options and canceled the OnTime event in the Yes and No answer and work around the generic "Save changes" prompt in excel.我制作了一个带有 vbYesNoCancel 选项的 Msgbox 并取消了 Yes 和 No 答案中的 OnTime 事件,并解决了 Excel 中的通用“保存更改”提示。 If Cancel is pressed the macro will cancel.如果按下取消,宏将取消。

The "If xRng.Value <= 1.15740740740741E-05 Then" in the beginning insures that if the timer has run out it skips the MsgBox and just saves.开头的“If xRng.Value <= 1.15740740740741E-05 Then”确保如果计时器用完它会跳过 MsgBox 并保存。

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Set xRng = Application.ThisWorkbook.Worksheets("kode").Range("H3")
If xRng.Value <= 1.15740740740741E-05 Then
Application.ScreenUpdating = False

ThisWorkbook.Worksheets("Interface").Select

'Hides all sheets but the interface sheet
Sheet2.Visible = False
Sheet3.Visible = False
Sheet6.Visible = False
Sheet7.Visible = False
Sheet8.Visible = False

Application.OnTime gCount, "ResetTime", Schedule:=False
If ThisWorkbook.Saved = False Then
    ThisWorkbook.Save
End If
Application.ScreenUpdating = True
GoTo Endsub
Else
End If


Dim intValue As Integer
intValue = MsgBox("Do you want to save changes?", 3, "Save changes?")

If intValue = 6 Then

Application.ScreenUpdating = False

ThisWorkbook.Worksheets("Interface").Select

'Hides all sheets but the interface sheet
Sheet2.Visible = False
Sheet3.Visible = False
Sheet6.Visible = False
Sheet7.Visible = False
Sheet8.Visible = False

Application.OnTime gCount, "ResetTime", Schedule:=False
If ThisWorkbook.Saved = False Then
    ThisWorkbook.Save
End If
Application.ScreenUpdating = True

ElseIf intValue = 7 Then

Application.ScreenUpdating = False

ThisWorkbook.Worksheets("Interface").Select

'Hides all sheets but the interface sheet
Sheet2.Visible = False
Sheet3.Visible = False
Sheet6.Visible = False
Sheet7.Visible = False
Sheet8.Visible = False

Application.OnTime gCount, "ResetTime", Schedule:=False
ThisWorkbook.Saved = True
Application.ScreenUpdating = True

Else
Cancel = True

End If
End Sub

Hope it can help someone with the same issue.希望它可以帮助有同样问题的人。

Best regars Søren最好的问候 Søren

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM