简体   繁体   English

是否可以根据另一个单元格中的值保护/取消保护一个单元格?

[英]Is it possible to protect/ unprotect a cell based on value in another cell?

I want to accomplish the following with conditional formatting (or some other tool in Excel),我想使用条件格式(或 Excel 中的其他工具)完成以下操作,

If the value in A1 is greater than 0, protect C1.如果 A1 中的值大于 0,则保护 C1。 If the value in A1 in smaller or equal to 0, unprotect c1.如果 A1 中的值小于或等于 0,则取消保护 c1。

I'm not sure if this is even possible because it only has number, fond, border and fill options when I tried to format the cell.我不确定这是否可能,因为当我尝试格式化单元格时,它只有数字、喜欢、边框和填充选项。

You could try using a VBA solution to this problem.您可以尝试使用 VBA 解决方案来解决此问题。

In order for this solution to work you need to format all cells on the given worksheet as being "unlocked" when the worksheet is "protected", except for cell C1 which needs to remain locked.为了使此解决方案起作用,您需要在工作表“受保护”时将给定工作表上的所有单元格格式化为“未锁定”,但需要保持锁定状态的单元格 C1 除外。

Insert the following code in the code module of the worksheet you want to have the cell locked on:在要锁定单元格的工作表的代码模块中插入以下代码:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Me.Range("A1").AddressLocal = Target.AddressLocal And _
            Not (Me.Range("A1").Value2 = 0) Then
        Me.Protect
    ElseIf Me.Range("A1").AddressLocal = Target.AddressLocal Then
        Me.Unprotect
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM