我有一個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
我在代碼中看不到'Activate'? –
嗨海倫,你正在描述一些基於代碼場景的問題條件,這些代碼場景似乎並不存在於你提供的代碼中。在您提供的代碼中沒有任何「Activate」語句。 –
糟糕!對不起大衛,輸入錯誤,Workbook_BeforeSave()中的「選擇」命令應該讀取「激活」!謝謝,海倫。 – HelenT