2017-08-29 132 views
0

我有一個Excel宏,它關閉文件前重置哪個工作表處於活動狀態如果我直接保存即執行ctrl-S ,激活工作,如果我試圖在關閉之前保存,激活不起作用在下面的代碼中,如果我做了一個直接的ctrl-S,然後Workbook_BeforeSave()運行 (Activate method works)。如果我嘗試關閉文檔,Workbook_BeforeClose()運行,然後當詢問「您要保存在關閉之前嗎?」時調用Workbook_Before Save()(激活不起作用),然後選擇「是」。工作表(「工作表1」)。激活並不總是工作:-(

爲什麼Activate在Workbook_BeforeSave )通過Workbook_BeforeClose()調用?

謝謝,海倫。

Private Sub Workbook_BeforeClose(Cancel As Boolean) 

Dim TempUserWantsToSaveFile As Integer 

Cancel = False 
If ThisWorkbook.Saved = False Then 
    'Workbook has changed since last save. 
    'Does user have permission to save the document? 
    ' Only prompt to save if the user is David, Rob or myself. 
    If (Environ("UserName") <> "dwilson") And _ 
     (Environ("UserName") <> "DWilson") And _ 
     (Environ("UserName") <> "RGrant") And _ 
     (Environ("UserName") <> "rgrant") And _ 
     (Environ("UserName") <> "HThompson") And _ 
     (Environ("UserName") <> "hthompson") Then 

     MsgBox "File will be closed without saving - you do not have permission to save it." 
    Else ' User is Rob, David or Helen. 
     TempUserWantsToSaveFile = MsgBox("Would you like to save before closing?", _ 
              vbYesNoCancel) 
     Select Case TempUserWantsToSaveFile 
      Case vbYes 
       ' ThisWorkbook.Save will call Workbook_BeforeSave() 
       MsgBox ("Now calling ThisWorkbook.Save") 
       ThisWorkbook.Save 

      Case vbNo 
       MsgBox ("DEBUG: file not saved") 

      Case vbCancel 
       MsgBox ("DEBUG: cancel file close") 
       Cancel = True 

      Case Else 
       'This is an error case - response should be yes, no or cancel. 
       MsgBox ("DEBUG: error: response should be yes, no or cancel.") 
     End Select 
    End If 

End If 
'else: workbook hasn't changed so just close the file 
End Sub 

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) 
' Only save this worksheet if the user is David, Rob or myself. 
MsgBox ("DEBUG: Workbook_BeforeSave") 
If (Environ("UserName") <> "dwilson") And _ 
    (Environ("UserName") <> "DWilson") And _ 
    (Environ("UserName") <> "RGrant") And _ 
    (Environ("UserName") <> "rgrant") And _ 
    (Environ("UserName") <> "HThompson") And _ 
    (Environ("UserName") <> "hthompson") Then 

    MsgBox "Sorry, only David and Rob can save this workbook!" 
    Cancel = True 
Else 
' Hide all sheets except Dummy Sheet before saving. 
' This will mean that if the workbook is opened with macros disabled, 
' the user will not be able to see other people's sheets. 

' Switch to the Dummy Sheet before continuing as you cannot 
' update the visible flag for the active worksheet: 
Worksheets("Dummy Sheet").Visible = xlSheetVisible 
Worksheets("Dummy Sheet").Activate 

    For Each sht In ThisWorkbook.Worksheets 
    ' Note the use of ThisWorkbook.Worksheets rather 
    ' than ThisWorkbook.Sheets. This is because .Sheets 
    ' contains both worksheets AND charts but "sht" is of 
    ' type Worksheet so cannot be a chart (code falls over 
    ' at any Chart sheet if .Sheets is used). 
     If sht.Name <> "Dummy Sheet" Then 

     sht.Visible = xlSheetVeryHidden 
     Debug.Print sht.Name 

     If sht.Name = "hthompson" Then MsgBox ("Hiding user sheet") 

     End If 
    Next 
Cancel = False 
MsgBox "DEBUG: Saving ..." 
End If 

End Sub 
+1

我在代碼中看不到'Activate'? –

+0

嗨海倫,你正在描述一些基於代碼場景的問題條件,這些代碼場景似乎並不存在於你提供的代碼中。在您提供的代碼中沒有任何「Activate」語句。 –

+0

糟糕!對不起大衛,輸入錯誤,Workbook_BeforeSave()中的「選擇」命令應該讀取「激活」!謝謝,海倫。 – HelenT

回答

0

猜1 - 你必須在你的代碼Cancel = False和你沒有意識到這一點。

猜測二 - 請嘗試以下的空的Excel:

Option Explicit 

Private Sub Workbook_BeforeClose(Cancel As Boolean) 

    Save 
    Cancel = True 

End Sub 

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) 

    Debug.Print "Before Save" 

End Sub 

如果沒有在控制檯打印「保存之前」,那麼你應該看看的事件。例如。 Application.EnableEvents = True

提示1: 更改您這樣的代碼來避免疊字:

ucase(Environ("UserName")) <> ucase("dwilson") 

因此有3條線路與ucase你在你的代碼覆蓋6。

+0

嗨Vityata,我把調試到我的代碼,它通過Workbook_BeforeSave(),但由於某種原因似乎並沒有採取行動的「激活」命令......當它作爲文件關閉事件的一部分運行。它用一個簡單的ctrl-S保存工作正常。任何想法爲什麼?謝謝,海倫。 – HelenT

+0

@HelenT - 你有沒有試過'Application.EnableEvents = True'? – Vityata

+0

嗨Vityata,剛剛嘗試設置Application.EnableEvents = True時打開文檔,但仍然無效:-(謝謝,海倫。 – HelenT