protect the worksheet but still can do the "group and outline" dat

  • Thread starter Thread starter lalann
  • Start date Start date
L

lalann

I need to set a password to protect the worksheet. In the meanwhile, I still
should be able to use "group and outline" in this worksheet. Could anyone
help to figure this out? Thanks.
 
If you already have the outline/subtotals/autofilter applied, you can protect
the worksheet in code (auto_open/workbook_open??).

Option Explicit
Sub auto_open()
With Worksheets("sheet1")
.Protect Password:="hi", userinterfaceonly:=True
.EnableOutlining = True
'.EnableAutoFilter = True
'If .FilterMode Then
' .ShowAllData
'End If
End With
End Sub

It needs to be reset each time you open the workbook. (Earlier versions of
excel don't remember it after closing the workbook. IIRC, xl2002+ will remember
the allow autofilter setting under tools|Protection|protect sheet, but that
won't help when you're filtering via code.)
 
Hi Dave,

Many thanks for your reply. However, I use your code, but it still doesn't
work.
I tried another way, and it works.

Private Sub Workbook_Open()
Worksheets("Sheet1").Protect Password:="hi", userinterfaceonly:=True
Worksheets("Sheet1").EnableOutlining = True

End Sub

I am not so familiar with code, so I don't know if these two codes mean the
same thing. Thanks anyway.
 
They do the same thing. My suggested code would be located in a General
module. Your code would go in the ThisWorkbook module.

I'm betting that you placed the Auto_Open procedure in the wrong location.
That's why it didn't work for you.
 
Yes, you are right. I put the code in the wrong location.
Finally it works! Awesome! Thanks so much.
 
Either version of the code (in the correct locations) will work.

But you don't want both versions running--it won't do much harm, but why do the
same thing twice?
Yes, you are right. I put the code in the wrong location.
Finally it works! Awesome! Thanks so much.
 
Back
Top