在关闭

时间:2015-09-04 01:41:28

标签: excel vba excel-vba

我是VBA和Excel的新手。我想知道,当Workbook_BeforeClose脚本运行时,我是否可以阻止消息框出现在单独的工作表上?下面将是Workbook_BeforeClose脚本,然后单独将是我不想要的单个工作表msgbox。我希望能够实现这一点,因此它不会让用户感到烦恼。建议?

附加信息:我主要的一切"关闭"脚本我想保持不变。我只是不希望单独的工作表对象也在关闭时激活。因为我的on close脚本会锁定仪表板,因此用户必须启用宏才能通过工作表并隐藏它们。好吧,在这个过程中,它还激活了该表,并显示了我不想要的"个人工作表对象的MSGBOX:"我希望它不会出现在" Workbook_BeforeClose:"被激活了。

Workbook_BeforeClose:

 Private Sub Workbook_BeforeClose(Cancel As Boolean)
 Dim ws As Worksheet
 Dim sDateTime As String, sFileName As String

 Sheets("START").Visible = xlSheetVisible
 For Each ws In ThisWorkbook.Worksheets
 If ws.Name <> "START" Then
 ws.Visible = xlVeryHidden
 End If
 Next ws
     Application.DisplayAlerts = False
     If ThisWorkbook.Readonly = True Then GoTo Passed2
     GoTo Passed3
 Passed2:
     If IsWorkbookOpen("CCC_Error_Tracker.xlsm") Then
         MsgBox "Excel has detected that your `Team Error Tracker` is still open and has not been saved. The Opportunities Dashboard will now close. As a reminder, in order to save your data, you must close ProcessID: `CCC_Error_Tracker.xlsm`", vbInformation
     End If
     GoTo End1:
 Passed3:
 CodeRetry:
     On Error GoTo Failed
         If Me.Saved = True And BackupReqd = False Then Exit Sub
     With ThisWorkbook
         sDateTime = " (" & Format(Now, "yyyy-mm-dd hhmm") & ").xlsm"
         sFileName = Replace(.Name, ".xlsm", sDateTime)
         .SaveCopyAs Filename:="P:\WI\Teams\Programs\J&J CCC\Care Specialist\Alicia's Team\FPA RESULTS\Supporting_Files\FPA_FILE_BACKUPS\Opportunities_Dashboard\" & sFileName
      GoTo Passed
 Failed:
  GoTo CodeRetry
 Passed:
 ThisWorkbook.Save
 MsgBox "Your data has been saved and backed-up successfully! Your backup will be stored for 72 hours before discarded to save disk space. Email Blank@Blank.com if you have a suggestion."
 GoTo End2
 End1:
     Application.Quit
 End2: 
 End With 
 End Sub

个人工作表对象:

 Private Sub Worksheet_Activate()
 If ThisWorkbook.Readonly = True Then GoTo Readonly
     GoTo Editmode
 Readonly:
 MsgBox "You're currently in read only mode and cannot save feedback. If you continue to provide feedback, you will lose all data on close."
 Editmode:
 End Sub

1 个答案:

答案 0 :(得分:2)

好的,让我们看看这是否会让你接近。可能还有其他方法,但这是一种方法。这仅用于演示和学习目的,因此我使用的是新工作簿而不是现有代码。希望您能看到它是如何工作的,并能够修改您的代码以满足您的需求。

步骤1:创建一个新工作簿并添加一些工作表(我的示例有3个)。

第2步:为每个工作表的Worksheeet_Activate事件添加一些代码。类似的东西:

Private Sub Worksheet_Activate()

    If Not ClosingEvent Then MsgBox Sheet1.Name

End Sub

修改每个工作表的工作表参考。

步骤3:添加一个新模块,在顶部放置:

Public ClosingEvent As Boolean

步骤4:在Workbook_BeforeClose事件中,添加以下代码:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim ws As Worksheet
    ClosingEvent = True

    For Each ws In ThisWorkbook.Worksheets
        ws.Activate
    Next ws

    ' this is here for testing, to keep the workbook from actually closing.
    Cancel = True
    ClosingEvent = False

End Sub

现在点击每个标签,看到该消息框?看看关闭工作簿时会发生什么,没有消息框!随意执行关闭事件,以便您可以看到每个工作表激活确实运行,但由于您将标志设置为仅在不是结束事件时运行,它会跳过消息框。如果您不相信我,请在BeforeClose事件中注释掉ClosingEvent = True行并重新运行代码......

希望有所帮助!如果您需要更多帮助,请回复。