Protection object (Excel)

Represents the various types of protection options available for a worksheet.


Use the Protection property of the Worksheet object to return a Protection object.

After a Protection object is returned, you can use the Protection properties to set or return protection options.


The following example demonstrates how to use the AllowInsertingColumns property of the Protection object, placing three numbers in the top row and protecting the worksheet. This example then checks to see if the protection setting for allowing the insertion of columns is False and sets it to True, if necessary. Finally, it notifies the user to insert a column.

Sub SetProtection() 
 Range("A1").Formula = "1" 
 Range("B1").Formula = "3" 
 Range("C1").Formula = "4" 
 ' Check the protection setting of the worksheet and act accordingly. 
 If ActiveSheet.Protection.AllowInsertingColumns = False Then 
 ActiveSheet.Protect AllowInsertingColumns:=True 
 MsgBox "Insert a column between 1 and 3" 
 MsgBox "Insert a column between 1 and 3" 
 End If 
End Sub


See also

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.