VBA将单元保护格式从解锁更改为锁定

时间:2015-11-18 21:28:44

标签: excel-vba vba excel

我的工作表包含被锁定以保护公式并解锁以允许数据输入的单元格。在工作表的末尾是审阅者的签名按钮。我想将VBA添加到此按钮,该按钮将锁定以前未锁定的单元格(按单元格格式),以防止在工作表审核完成后进行更改。

这是我目前的代码 -

Sub Button9_Click()
If Range("I108") = Empty Then
If MsgBox("Are you sure you want to sign-off COMPLETE as Reviewer?", vbYesNo) = vbNo Then Exit Sub
ActiveWorkbook.ActiveSheet.Unprotect "locked"
ActiveWorkbook.ActiveSheet.Range("I109").Value = "Reviewed: " & Format(Date, "mm/dd/yyyy") & " By: " & Application.UserName
ActiveWorkbook.ActiveSheet.Protect "locked"
End If
End Sub

2 个答案:

答案 0 :(得分:1)

这就是你的意思:

ActiveWorkbook.ActiveSheet.Cells.Locked = True

这会将工作表中的所有单元格设置为锁定

答案 1 :(得分:0)

问题似乎是由工作簿中的事件触发的。试试这个:

Sub Button9_Click()
Const kPassWord As String = "locked"
Const kCll As String = "I109"

    With ActiveWorkbook.ActiveSheet
        If .Range(kCll) = Empty Then
            If MsgBox("Are you sure you want to sign-off COMPLETE as Reviewer?", vbYesNo) = vbNo Then Exit Sub
            Application.EnableEvents = False
            .Unprotect kPassWord
            .Range(kCll).Value = "Reviewed: " & _
                Format(Date, "mm/dd/yyyy") & " By: " & Application.UserName
            .Cells.Locked = True
            .Cells.FormulaHidden = False
            .Protect kPassWord
            Application.EnableEvents = True
    End If: End With

End Sub