繁体   English   中英

根据其他单元格上的值锁定单元格

[英]Locking cell based on value on other cell

请找到下面的代码。 我不确定我的代码在哪里出错。 我的目的是检查D44单元格(下拉列表)中的值并基于值锁定/解锁D45单元格。

D44中的值为NO,13 fracto,18 fracto,Any Other Fracto”仅当D44单元格值设置为“ Any Other Fracto”时,用户才应该能够编辑单元格D45。

Sub TheSelectCase()
    Select Case Range("D44").Value
          Case "Any Other Fracto"
          Range("D45").Locked = False
          Range("D45").Activate

          Case = "NO" 
           Range("D45").Locked = True
       Range("D45").Clear

      Case <> "NO" Or "Any Other Fracto"
      Range("D45").Locked = True
    End Select
End Sub

除非下拉列表是ActiveX控件,否则您将需要通过Worksheet_Change事件处理程序执行此操作,并且,您只有两种情况:1)允许用户进行编辑的情况(“其他任何分数”)和2) 任何情况不允许用户编辑的其他值

假设您正在使用验证列表,请在工作表的Worksheet_Change事件中执行以下操作:

Private Sub Worksheet_Change(ByVal Target As Range)

    'Exit on any other cell
    If Intersect(Target, Range("D44")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    Dim inputCell As Range
    Set inputCell = Range("D45")
        Select Case Trim(LCase(Target.Value))
            Case "any other fracto"
                inputCell.Locked = False
                inputCell.Activate
            Case Else
            'This handles **ANY** other value in the dropdown
                inputCell.Locked = True
                InputCell.Clear
        End Select

   Application.EnableEvents = True
End Sub

暂无
暂无

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

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