Protection 对象

         
Worksheet
Protection
AllowEditRanges

代表工作表可使用的各种保护选项类型。

Protection 对象用法

可用 Worksheet 对象的 Protection 属性返回一个 Protection 对象。

返回一个 Protection 对象后,就可用该对象的下列属性来设置或返回保护选项。

下例说明如何使用 Protection 对象的 AllowInsertingColumns 属性,在首行中放置三个数并保护工作表。然后本示例检查允许插入列的保护设置是否为 False,并在需要时将其设置为 True。最后,通知用户插入一列。

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