enable formatting rows through Thsworkbook VBA code

I originally wrote code in ThisWorkbook to allow the user to use the grouping function in a protected worksheet using the following code.

Private Sub Workbook_Open()
With Worksheets("Jobs By Pursuit Manager")
       .Protect Password:="pursuits1", Userinterfaceonly:=True
       .EnableOutlining = True
End With
End Sub

I then realized that even if I saved the file with autofilters and formatting rows allowed for the sheet, these attributes were removed upon opening the file.  

I was able to add .EnablAutoFilter = True to the code, as shown below, but I cant seem to get the allowance of formatting rows to work.  How do I add this to my code?

Private Sub Workbook_Open()
With Worksheets("Jobs By Pursuit Manager")
       .Protect Password:="pursuits1", Userinterfaceonly:=True
       .EnableOutlining = True
       .EnableAutoFilter = True
End With
End Sub
rob heibelAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rob HensonFinance AnalystCommented:
Try this:

Private Sub Workbook_Open()
With Worksheets("Jobs By Pursuit Manager")
       .Protect Password:="pursuits1", Userinterfaceonly:=True
       .EnableOutlining = True
       .EnableAutoFilter = True
       .AllowFormattingRows:=True
End With
End Sub

Open in new window

0
ShumsDistinguished Expert - 2017Commented:
Try below:
Private Sub Workbook_Open()
Dim Ws As Worksheet
Dim strPassword As String
Set Ws = worksh("Jobs By Pursuit Manager")
strPassword = "pursuits1"
With Ws
    .Protect Password:=strPassword, Contents:=True, Userinterfaceonly:=True, AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows:=True, AllowDeletingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True, DrawingObjects:=True, Scenarios:=True
    .EnableOutlining = True
    .EnableAutoFilter = True
End With
End Sub

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rob heibelAuthor Commented:
Rob, using  ".AllowFormattingRows:=True"  creates a Compile error: "Expected: expression".

By using my original code, but then using Schum's technique of adding ", AllowFormattingRows"  after userinterfaceonly:=True, it works perfectly.  I ended up using the following:

Private Sub Workbook_Open()
With Worksheets("Jobs By Pursuit Manager")
       .Protect Password:="pursuits1", Userinterfaceonly:=True, AllowFormattingRows:=True
       .EnableOutlining = True
       .EnableAutoFilter = True
End With

End Sub
0
rob heibelAuthor Commented:
By using my original code, but then using Schum's technique of adding ", AllowFormattingRows"  after userinterfaceonly:=True, it works perfectly.  I ended up using the following:

Private Sub Workbook_Open()
With Worksheets("Jobs By Pursuit Manager")
       .Protect Password:="pursuits1", Userinterfaceonly:=True, AllowFormattingRows:=True
       .EnableOutlining = True
       .EnableAutoFilter = True
End With

End Sub
0
ShumsDistinguished Expert - 2017Commented:
You're Welcome Rob! Glad it worked.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.