Excel - 如何使用 VBA 有条件地锁定和解锁特定单元格。

Excel - How to conditionally lock and unlock a particular cell using VBA.

标题说明了一切。这里有什么见解吗?此代码绝对无效:

Sub MrFreeze(ByVal Target As Range)

 Dim cCell As Range
 Dim wksInput As Worksheet

 Set wksInput = Worksheets("Input")
 Set cCell = wksInput.Range("D14")

 If cCell.Value = "Yes" Then
    ActiveSheet.Unprotect Password = "password"
    Else
    ActiveSheet.Protect Password = "password", DrawingObjects:=True, Contents:=True, Scenarios:=True
 End If

End Sub

提前感谢您的帮助!

如果单元格D14包含并且宏是运行,单元格将被解锁
如果单元格D14不包含且宏为运行,单元格将被锁定

Sub MrFreeze()

   Dim cCell As Range
   Dim wksInput As Worksheet

   Set wksInput = Worksheets("Input")
   Set cCell = wksInput.Range("D14")

   If cCell.Value = "Yes" Then
      ActiveSheet.Unprotect Password = "password"
         cCell.Locked = False
      ActiveSheet.Unprotect Password = "password"
   Else
      ActiveSheet.Unprotect Password = "password"
         cCell.Locked = True
      ActiveSheet.Protect Password = "password"
   End If

End Sub