我是VBA和Excel的新手。我想知道是否有消息,當Workbook_BeforeClose腳本運行時,我可以防止消息框出現在近距離和單個工作表中?下面是Workbook_BeforeClose腳本,然後將單獨的工作表msgbox在我不需要時運行。我希望能夠實現這一點,因此對於接近用戶來說不會感到煩惱。建議?防止在關閉時激活Workbook_BeforeClose時從單張彈出MSGBOX
附加信息:一切都在我的主「在接近」劇本我想保持不變。我只是不希望個人工作表對象也關閉以及激活。因爲我在關閉腳本上鎖定了儀表板,因此用戶被迫啓用宏,以便通過表單並隱藏它們。那麼在這個過程中,它也激活了該表並顯示了我不希望從「個人工作表對象」中找到的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 [email protected] 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
你有沒有嘗試刪除這些線?我敢打賭,這將阻止他們出現... – sous2817
你是什麼意思?也許我沒有正確解釋。在我的主要「關閉」腳本中的所有內容我都希望保持不變。我只是不希望個人工作表對象也關閉以及激活。因爲我在關閉腳本上鎖定了儀表板,因此用戶被迫啓用宏,以便通過表單並隱藏它們。那麼在這個過程中,它也激活了該表並顯示了我不希望從「個人工作表對象」中找到的MSGBOX:「我希望它在」Workbook_BeforeClose:「激活時不顯示。讓我知道這是否更有意義? –
我希望我有時間模擬一個例子,但不幸的是我沒有。你可以採取的一種方法是在關閉事件開始時有一個公共布爾變量,稱爲「ClosingEvent」,它被設置爲True。然後通過在msgbox周圍添加If Not ClosingEvent語句輕微修改Worksheet_Activate事件。這樣,它只會在ClosingEvent = False時執行(這是初始化時的默認值)。如果沒有人提出更好的解決方案,我會盡快在我獲得免費分鐘時嘲笑某些事情。 – sous2817