Protecting a workbook on close but allowing certain formats

Andy & Kate

New Member
Joined
May 11, 2005
Messages
14
I have written a macro to unfilter a workbook when it is saved but it also needs to unprotect and then protect up the workbook. My problem is that the protection needs to allow certain formatting. I have tried the following but it doesn't allow the formating:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Sheets("OUTPUTS").Select
ActiveSheet.Unprotect ("investment")
If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData
ActiveSheet.Protect ("investment")
AllowFormattingCells = True
AllowFormattingColumns = True
AllowFormattingRows = True
AllowFiltering = True
AllowEditRanges = True

End Sub

Any suggestions would be greatly appreciated.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
All of those formats you want to allow are different parameters of the Protect statement and they have to be on the same line--they can't be separated like you currently have them. Plus, the "AllowFormatCells" etc. are labels. You have to use a ":=" between the label and the value if you want to use them.

Untested, but try:
Code:
With Sheets("OUTPUTS")
    .Unprotect ("investment")
    If .FilterMode Then .ShowAllData
        .Protect Password:="investment", AllowFormattingCells:=True, _
        AllowFormattingColumns:=True, _
        AllowFormattingRows:=True, AllowFiltering:=True, _
        AllowEditRanges:=True
    End If
End With
 
Upvote 0

Forum statistics

Threads
1,206,710
Messages
6,074,459
Members
446,071
Latest member
Jolon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top