2017-08-29 130 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