Protecting Worksheets

All the arguments are optional

ActiveSheet.Protect Password:="", _ 
                    DrawingObjects:=True, _
                    Contents:=True, _
                    Scenarios:=True
                    UserInterfaceOnly, _
                    AllowFormattingCells, _
                    AllowFormattingColumns, _
                    AllowFormattingRows, _
                    AllowInsertingColumns, _
                    AllowInsertingRows, _
                    AllowInsertingHyperlinks, _
                    AllowDeletingColumns, _
                    AllowDeletingRows, _
                    AllowSorting, _
                    AllowFiltering, _
                    AllowUsingPivotTables)

Password - A string that specifies a case-sensitive password for the worksheet or workbook. If this argument is omitted, you can unprotect the worksheet or workbook without using a password. Otherwise, you must specify the password to unprotect the worksheet or workbook. If you forget the password, you cannot unprotect the worksheet or workbook. It's a good idea to keep a list of your passwords and their corresponding document names in a safe place.
DrawingObjects - True to protect shapes. The default value is False.
Contents - True to protect contents. For a chart, this protects the entire chart. For a worksheet, this protects the locked cells. The default value is True.
Scenarios - True to protect scenarios. This argument is valid only for worksheets. The default value is True.
UserInterfaceOnly - True to protect the user interface, but not macros. If this argument is omitted, protection applies both to macros and to the user interface.
AllowFormattingCells - True allows the user to format any cell on a protected worksheet. The default value is False.
AllowFormattingColumns - True allows the user to format any column on a protected worksheet. The default value is False.
AllowFormattingRows - True allows the user to format any row on a protected. The default value is False.
AllowInsertingColumns - True allows the user to insert columns on the protected worksheet. The default value is False.
AllowInsertingRows - True allows the user to insert rows on the protected worksheet. The default value is False.
AllowInsertingHyperlinks - True allows the user to insert hyperlinks on the worksheet. The default value is False.
AllowDeletingColumns - True allows the user to delete columns on the protected worksheet, where every cell in the column to be deleted is unlocked. The default value is False.
AllowDeletingRows - True allows the user to delete rows on the protected worksheet, where every cell in the row to be deleted is unlocked. The default value is False.
AllowSorting - True allows the user to sort on the protected worksheet. Every cell in the sort range must be unlocked or unprotected. The default value is False.
AllowFiltering - True allows the user to set filters on the protected worksheet. Users can change filter criteria but can not enable or disable an auto filter. Users can set filters on an existing auto filter. The default value is False.
AllowUsingPivotTables - True allows the user to use pivot table reports on the protected worksheet. The default value is False.
With the exception of the AllowEditRanges property, the Protection object's properties are set when you use the Protect method to protect a worksheet.


If you apply the Protect method with the UserInterfaceOnly argument set to True to a worksheet and then save the workbook, the entire worksheet (not just the interface) will be fully protected when you reopen the workbook. To re-enable the user interface protection after the workbook is opened, you must again apply the Protect method with UserInterfaceOnly set to True.
If changes wanted to be made to a protected worksheet, it is possible to use the Protect method on a protected worksheet if the password is supplied. Also, another method would be to unprotect the worksheet, make the necessary changes, and then protect the worksheet again.
Note 'Unprotected' means the cell may be locked (Format Cells dialog) but is included in a range defined in the Allow Users to Edit Ranges dialog, and the user has unprotected the range with a password or been validated via NT permissions.




Accessing Properties

ActiveSheet.Protection.AllowDeletingColumns = False 
ActiveSheet.Protection.AllowDeletingRows = False
ActiveSheet.Protection.AllowFormattingCells = False
ActiveSheet.Protection.AllowFormattingRows = False
ActiveSheet.Protection.AllowFormattingColumns = False
ActiveSheet.Protection.AllowInsertingRows = False
ActiveSheet.Protection.AllowInsertingColumns = False
ActiveSheet.Protection.AllowInsertingHyperlinks = False
ActiveSheet.Protection.AllowFiltering = False
ActiveSheet.Protection.AllowSorting = False
ActiveSheet.Protection.AllowEditRanges = False
ActiveSheet.Protection.AllowUsingPivotTables = False


Protecting Worksheets - Features Disabled

Even though this option is automatically disabled for a worksheet that is protected the following line of code does work.
0) (Edit > Delete)


1) (Edit > Links)


2) (Edit > Object)



3) (Format > Cells)(Alignment tab)("Merge cells")

Selection.MergeCells = True 

4) (Insert > Name)



5) (Insert > Picture)
The From File command allows you insert a graphic from a file.
This line of code will not work when the worksheet is protected

ActiveSheet.Pictures.Insert("C:\Temp\Picture.bmp") 

The Autoshapes command just displays the Drawing toolbar and the AutoShapes toolbar and these can be displaying manually using (View > Toolbars > Drawing)





© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext