我有一个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