我使用一些基于工作表事件和模块的宏来构建内置计时器。如果某人在XX分钟内未在文件中创建任何版本,它将被保存并关闭。它通常可以正常工作,但是代码有时会在ws.Visible = xlVeryHidden
我多次复制它来查找问题的根源。 仅当该工作簿未处于活动状态时,它才会中断。如果我与其他工作簿一起工作,并且计时器按XX,则会破坏我的代码。
我需要这些循环才能将工作表设置为 START值。
激活我的用户强制宏很重要。用户只能在开始的工作表“ START” 上看到。如果他们选择激活所有宏。工作表“开始”将被隐藏,其他将显示。
模块宏:
Option Explicit
Dim CloseTime As Date
Sub TimeSetting()
CloseTime = Now + TimeValue("00:00:30")
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=True
End Sub
Sub TimeStop()
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=False
End Sub
Sub SavedAndClose()
Dim ws As Worksheet
'Step 1: Unhide the Starting Sheet
Sheets("START").Visible = xlSheetVisible
For Each ws In ThisWorkbook.Worksheets
'Step 2: Check each worksheet name
If ws.Name <> "START" Then
ws.Visible = xlVeryHidden 'Step 3: Hide the sheet
End If
Next ws 'Step 4: Loop to next worksheet
ActiveWorkbook.Close Savechanges:=True
End Sub
工作簿宏:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call TimeStop
ActiveWorkbook.Save
End Sub
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Visible = xlSheetVisible
Next ws
Sheets("START").Visible = xlVeryHidden
Call TimeSetting
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Call TimeStop
Call TimeSetting
End Sub
答案 0 :(得分:0)
Sub SavedAndClose()
Dim ws As Worksheet
Application.ScreenUpdating = False
'Step 1: Unhide the Starting Sheet
On Error GoTo Error
Sheets("START").Visible = xlSheetVisible
'Sheets("START").Select
For Each ws In ThisWorkbook.Worksheets
'Step 2: Check each worksheet name
If ws.Name <> "START" Then
'Step 3: Hide the sheet
ws.Visible = xlVeryHidden
End If
'Step 4: Loop to next worksheet
Next ws
Error:
Sheets("START").Visible = xlSheetVisible
Application.ScreenUpdating = True
ThisWorkbook.Close Savechanges:=True
End Sub