我有一个Excel宏,它会在关闭文件之前重置哪个工作表处于活动状态。如果我直接保存,即通过执行ctrl-S,激活工作。如果我在关闭前尝试保存,则激活不起作用。在下面的代码中,如果我执行直接ctrl-S,则运行Workbook_BeforeSave() (激活方法有效)。如果我尝试关闭文档,则运行Workbook_BeforeClose(),然后在询问“是否要在关闭前保存?”时选择“是”。调用Workbook_Before Save()(激活不起作用)。
为什么通过Workbook_BeforeClose()调用Workbook_BeforeSave()时,Activate不起作用?
谢谢,海伦。
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
答案 0 :(得分:0)
猜1 - 您的代码中有Cancel = False
,而您却不知道。
猜猜2 - 在空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
如果您不打印&#34;在保存之前&#34;在控制台中,你应该看看事件。例如。 Application.EnableEvents = True
。
提示1 : 像这样更改代码以避免重复:
ucase(Environ("UserName")) <> ucase("dwilson")
因此,使用ucase
的3行代码可以覆盖代码中的6行。