Share via

Setting Protect Sheet parameters using VBA

Anonymous
2015-07-09T15:02:32+00:00

I'm a novice to macros and am using one I found on this site to allow grouped rows and columns to be expanded and collapsed on 2 protected worksheets in my workbook.  The macro unprotects then reprotects the sheets, but when it reprotects them, some of the Protect Sheet permissions I previously checked are unchecked; only 'Select locked cells' and 'Select unlocked cells' are checked (I'm assuming that's because those are the default parameters?). I want to allow users to format cells, columns and rows as well.   Is it possible to add code to my macro to retain the Protect Sheet permissions that I set initially?  Here's the macro I'm using:

Private Sub Workbook_Open()

With Worksheets(“worksheet 1”)

.Unprotect "password"

.EnableOutlining = True

.Protect "password", contents:=True, userInterfaceOnly:=True

End With

With Worksheets(“worksheet 2”)

.Unprotect "password"

.EnableOutlining = True

.Protect "password", contents:=True, userInterfaceOnly:=True

End With

End Sub

Thank you.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2015-07-09T15:33:02+00:00

Hi,

Try this and the same for the other worksheet

With Worksheets("worksheet 1")

    .Unprotect "password"

    .Protect "password", DrawingObjects:=True, contents:=True, Scenarios:=True _

        , AllowFormattingCells:=True, AllowFormattingColumns:=True, _

        AllowFormattingRows:=True

End With

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Anonymous
    2015-07-09T17:26:49+00:00

    Thank you Mike H.  Your solution works perfectly!

    Was this answer helpful?

    0 comments No comments