Excel - 如果可能,如何在不使用宏的情况下锁定单元格

use*_*124 1 excel

我有使用宏实现的解决方案,但我想删除它,如果可以的话.所以问题就在这里

我在单元格上使用验证规则实现了下拉列表.我希望该单元格是只读的,具体取决于同一张纸上第二个单元格中的值.

我试图锁定使用另一个验证,但它不允许我.

任何的想法?

Sid*_*out 6

我假设数据验证在单元格中A2,您检查的值在Cell中A1

当您将单元格的值更改A1为"Blah Blah"时,代码将运行,然后锁定单元格A2.在实际运行之前,请花点时间阅读代码中的注释.

代码必须粘贴在工作表代码区域中.

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub

    Application.EnableEvents = False

    Dim mypassword As String, StringToCheck As String

    On Error GoTo Whoa

    '~~> Change the password here to protect/unprotect the sheet
    mypassword = "password"

    '~~> Change it to the relevant string with which you want to compare
    StringToCheck = "Blah Blah"

    If Not Intersect(Target, Range("A1")) Is Nothing Then
        '~~> Check for the cell value
        If Target.Value = StringToCheck Then
            '~~> Un-Protect the sheet
            ActiveSheet.Unprotect mypassword

            '~~> Lock the cell
            Range("A2").Locked = True

            '~~> Re-Protect the sheet
            ActiveSheet.Protect mypassword
        End If
    End If

LetsContinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub
Run Code Online (Sandbox Code Playgroud)


JMa*_*Max 6

您可以阻止用户仅使用输入新数据 Data Validation

编辑:在列表中使用公式

感谢Catering主管的评论,值得记住的是,你可以使用数据验证/列表的公式.

看到他对另一个线程的回答,看看这个行动:https://stackoverflow.com/a/11902463/138938

原帖

Excel 2007演练:

  • 功能区>数据>数据验证
  • 授权:个性化(或类似,我的Excel不是英文抱歉)
  • 在字段中输入以下公式:
    • =IF(A1="",FALSE,TRUE)

因此,如果A1仍为空,则无法在单元格中输入值