Excel:在打开工作簿

时间:2018-04-23 14:30:21

标签: excel-vba grouping vba excel

与许多其他人一样,我希望能够使用受保护工作表上的小+/-按钮启用分组和取消分组。每个人似乎都成功使用相同类型的代码保护工作表,启用概述然后再保护它,这很好,除非我保存工作表然后再次重新打开它,否则它仍然有效,EnableOutlining总是设置为False,如果工作表受到保护,我不能使用+/-按钮。我是否应该做一些永久保存此设置的事情,而不仅仅是在会话期间?

这是我一直在使用的代码:

Private Sub Workbook_Open()
MsgBox ActiveSheet.EnableOutlining
End Sub

Sub EnableOutliningWithProtection_AllSheets()
'PURPOSE: Allow Outline functionality during Protection in all Sheets
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
'(Except edited by me to include the Errorcatch)

Dim sht As Worksheet
On Error GoTo Errorcatch
'Loop through each Worksheet in ActiveWorkbook
  For Each sht In ActiveWorkbook.Worksheets
    'Password Protect Current Sheet
      sht.Protect Password:="", UserInterfaceOnly:=True

    'Enable Group Collapse/Expand Capabilities
      sht.EnableOutlining = True

    'Unprotect Sheet
      sht.Unprotect ""

  Next sht

Exit Sub
Errorcatch:
MsgBox Err.Description

End Sub

(我有Workbook_Open()位来检查EnableOutlining是否仍为True) 我已经看到'保护UserInterfaceOnly和EnableOutlining'的问题,但是我不认为应用的结果是代码是为C#编写的,我并不是在考虑保护UserInterfaceOnly。

1 个答案:

答案 0 :(得分:2)

您无法永久保存。您必须使用Open事件在工作簿打开时重置它。

Private Sub Workbook_Open()
    EnableOutliningWithProtection_AllSheets
End Sub