【发布时间】:2015-12-07 09:06:28
【问题描述】:
我有一个包含 6 个用户表单的电子表格,在大约 30 台计算机上使用。 VBA 代码受密码保护。通常当我们关闭工作表时,会出现 VBA 项目密码框,并且 excel.exe 保留在任务管理器中。
我做了一些测试,得出以下结论:
- 只有在打开用户窗体时才会出现此问题。
- 除了按 Cancel(调用 Unload Me)外,无需对用户窗体执行任何操作即可导致弹出窗口
Workbook_BeforeClose 事件如下:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.ScreenUpdating = False
'Cancel autosave
Sheets("BOH General").Range("A102").Value = 0
AutoSaveTimer
'Application.EnableEvents = False
If Not Sheets("START").Visible = True Then Call CostingMode
Call BackItUp
'Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
以下是 Workbook_BeforeClose 调用的其他一些宏:
Sub AutoSaveTimer()
If Sheets("BOH General").Range("A102").Value > 0 Then
RunWhen = Now + TimeSerial(0, Sheets("BOH General").Range("A102").Value, 0)
Application.OnTime EarliestTime:=RunWhen, Procedure:="AutoSaveIt", _
Schedule:=True
Else
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, Procedure:="AutoSaveIt", _
Schedule:=False
On Error GoTo 0
End If
End Sub
Sub AutoSaveIt()
ThisWorkbook.Save
Call AutoSaveTimer
End Sub
Sub BackItUp()
'Dont run if unsaved
If Sheets("BOH General").Range("A111").Value = "" Then Exit Sub
'Prompt
If MsgBox("Do you want to backup this sheet now (recommended if you made any changes)?", vbYesNo) = vbNo Then Exit Sub
'reformat date
Dim DateStamp As String
DateStamp = Format(Now(), "yyyy-mm-dd hh-mm-ss")
On Error Resume Next
MkDir ActiveWorkbook.Path & "\" & "Backup"
On Error GoTo 0
ActiveWorkbook.SaveCopyAs (ActiveWorkbook.Path & "\" & "Backup" & "\" & ActiveWorkbook.Name & " - backup " & DateStamp & ".xlsb")
ActiveWorkbook.Save
End Sub
这是一个用户表单错误,用户表单没有正确关闭吗?还是别的什么?
更新:此错误仅在用户单击 excel 关闭按钮(右上角)单击文件>关闭不会产生错误后发生。
【问题讨论】:
-
完全随机猜测但可能是personal.xlsb?
-
我很确定如果有 .OnTime() 事件排队,Excel 无法真正完全关闭。
-
@findwindow,您能否扩展该评论?
-
@ExcelHero,该代码应该在工作表关闭之前停止 ontime 事件。无论如何,在我进行测试时,我没有启用自动保存功能