Home » excel » grouping – Excel: EnableOutlining seems to default to False on opening workbook

grouping – Excel: EnableOutlining seems to default to False on opening workbook

Posted by: admin May 14, 2020 Leave a comment

Questions:

Like many other people, I want to be able to enable grouping and ungrouping with the little +/- buttons on a protected worksheet. Everyone seems to have succeeded with the same sort of code that protects the worksheet, enables outlining and then unprotects it again, which is great and it works except if I save the sheet and then re-open it again EnableOutlining is always set as False, and if the sheet is protected I cannot use the +/- buttons. Is there something else I am supposed to do to save this setting permanently, and not just for the duration of the session?

Here’s the code I have been using:

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

(I’ve got the Workbook_Open() bit to check if EnableOutlining was still True)
I’ve seen the ‘protect UserInterfaceOnly and EnableOutlining’ question, but I didn’t think the results applied as the code was written for C#, and I’m not looking at protecting UserInterfaceOnly.

How to&Answers:

You can’t save it permanently. You have to use the Open event to reset it when the workbook is opened.

Private Sub Workbook_Open()
    EnableOutliningWithProtection_AllSheets
End Sub