[英]Excel Macro causes file to re-open
I have an odd issue with a macro I put together for work. 我为工作而放在一起的宏有一个奇怪的问题。
Backstory, I basically designed a macro to close a protected-workbooks after being left opened after X amount of time. 在后台,我基本上设计了一个宏来关闭受保护的工作簿,该工作簿在X时间后被打开。 its a shared document, and when people open it, they often forget to close the document, locking it up so others can't use it.
它是一个共享文档,当人们打开它时,他们通常会忘记关闭文档,将其锁定,以致其他人无法使用它。
So I took bits and pieces of other macros to create a timer. 因此,我花了一点点其他宏来创建计时器。
Sub Start_Timer()
'BASIC UP COUNTER
Application.OnTime Now() + TimeValue("00:00:01"), "CheckStatus"
End Sub
Sub CheckStatus()
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Range("IV1").Value > 60 Then
'IF IDLE LONGER THAN VALUE ABOVE, RUN THE FOLLOWING
Application.DisplayAlerts = False 'WORKS TO DISABLE ALERT PROMPT
ThisWorkbook.Close (False) 'CLOSE FILE WITHOUT SAVING
Application.Quit
Application.DisplayAlerts = True 'RESETS DISPLAY ALERTS
End Sub
Else
'ADD 1 TO COUNTER
ws.Range("IV1").Value = ws.Range("IV1").Value + 1
End If
Next
Start_Timer
End Sub
in ThisWorkbook 在本工作簿中
Private Sub Workbook_Open()
Dim ws As Worksheet
MsgBox ("This Worksheet has a 15 min timer")
Start_Timer
For Each ws In Worksheets
'Columns("IV:IV").Hidden = True
ws.Range("IV1").Value = 1
Next
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim ws As Worksheet
For Each ws In Worksheets
ts.Range("IV1").Value = 1
Next
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.DisplayAlerts = False 'WORKS TO DISABLE ALERT PROMPT
ThisWorkbook.Close (False) 'CLOSE FILE WITHOUT SAVING
Application.Quit
Application.DisplayAlerts = True 'RESETS DISPLAY ALERTS
End Sub
For the most part it works. 在大多数情况下,它都可以工作。 But, I'd rather eliminate the like "Application.Quit".
但是,我宁愿删除“ Application.Quit”之类的东西。 The problem I am having is that when I eliminate that line, the Workbook instantly re-opens after closing it manually, or trying to quit excel manually.
我遇到的问题是,当我消除该行时,在手动关闭它或尝试手动退出excel后,工作簿会立即重新打开。
I assume it might have to do with constantly changing the value of 1 cell (far off so no one sees it) and exiting without saving. 我认为这可能与不断更改1个单元格的值(相距甚远,所以没人能看到它)并退出而不保存有关。
Can anyone help me? 谁能帮我? Thanks
谢谢
Maybe you have a typo?: 也许您有错字?:
Dim ws As Worksheet
For Each ts In Worksheets
^ws ??
If ws.Range("IV1").Value > 60 Then
You know you can give an Excel file a shared status, so that it can be accessed simultaniously by multiple users? 您知道可以为Excel文件指定共享状态,以便多个用户同时访问它吗?
see for example: http://office.microsoft.com/en-in/excel-help/share-a-workbook-HP005202595.aspx 参见例如: http : //office.microsoft.com/en-in/excel-help/share-a-workbook-HP005202595.aspx
Add a global variable to store the last scheduled OnTime() time value and adjust your timer sub: 添加一个全局变量以存储最后调度的OnTime()时间值并调整您的计时器子:
Dim lastTimer As Variant
Sub Start_Timer()
'BASIC UP COUNTER
lastTimer = Now() + TimeValue("00:00:01")
Application.OnTime lastTimer, "CheckStatus"
End Sub
Then add this to the Workbook_BeforeClose event: 然后将其添加到Workbook_BeforeClose事件:
Application.OnTime lastTimer, "CheckStatus", , False
This will cancel the last scheduled call by Application.OnTime(). 这将通过Application.OnTime()取消上一次计划的调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.