Protection Object

Microsoft Excel Visual Basic

Protection Object

Worksheet Protection
AllowEditRanges

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

Using the Protection object

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

Once a Protection object is returned, you can use its following 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. Then this example 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"
    ActiveSheet.Protect

    ' 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"
    Else
        MsgBox "Insert a column between 1 and 3"
    End If

End Sub