2015-09-04 47 views
1

我是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 
+0

你有沒有嘗試刪除這些線?我敢打賭,這將阻止他們出現... – sous2817

+0

你是什麼意思?也許我沒有正確解釋。在我的主要「關閉」腳本中的所有內容我都希望保持不變。我只是不希望個人工作表對象也關閉以及激活。因爲我在關閉腳本上鎖定了儀表板,因此用戶被迫啓用宏,以便通過表單並隱藏它們。那麼在這個過程中,它也激活了該表並顯示了我不希望從「個人工作表對象」中找到的MSGBOX:「我希望它在」Workbook_BeforeClose:「激活時不顯示。讓我知道這是否更有意義? –

+0

我希望我有時間模擬一個例子,但不幸的是我沒有。你可以採取的一種方法是在關閉事件開始時有一個公共布爾變量,稱爲「ClosingEvent」,它被設置爲True。然後通過在msgbox周圍添加If Not ClosingEvent語句輕微修改Worksheet_Activate事件。這樣,它只會在ClosingEvent = False時執行(這是初始化時的默認值)。如果沒有人提出更好的解決方案,我會盡快在我獲得免費分鐘時嘲笑某些事情。 – sous2817

回答

2

OK,讓我們來看看,如果這讓你關閉。可能還有其他方法,但是這裏有一條路要走。這僅僅是爲了演示和學習的目的,所以我使用的是新的工作簿而不是現有的代碼。希望你會看到它是如何工作的,並且能夠修改你的代碼以滿足你的需求。

第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 =真行並重新運行該代碼...

希望幫助!如果您需要更多幫助,請回復。

+0

非常感謝你! –