繁体   English   中英

根据其他单元格输入锁定Excel 2013单元格

[英]Lock Excel 2013 cells based on other cell input

我有一个工作表,其中有许多不同的列供输入。 有问题的列是D,H和I。D和H是下拉列表。 我是任何文字输入。 我试图将VBA脚本放在一起,以便在D2中进行选择时将锁定H2和I2。 如果在H2中进行了选择,则将锁定D2和I2。 如果在I2中输入文本,则将锁定D2和H2。 最后,将需要对D,H和I的整个列进行此操作,以使这些列中的每个单元格都具有相同的属性,即,如果I16被填充等等,D16和H16将锁定。

如果这也可以通过公式来实现,我不介意。

  `  Private Sub Worksheet_Change(ByVal Target As Cells)
            If ActiveSheet.Cells(2, 4).Text = True Then 
'This is what I don't understand. I don't know what to set the text to. I'm trying to say if there's anything in the cell Then do the following...
                ActiveSheet.Cells(2, 8).Locked = True
                ActiveSheet.Cells(2, 9).Locked = True
            Else
                ActiveSheet.Cells(2, 8).Text = True Then
                ActiveSheet.Cells(2, 4).Locked = True
                ActiveSheet.Cells(2, 9).Locked = True
            Else
                ActiveSheet.Cells(2, 9).Text = True Then
                ActiveSheet.Cells(2, 4).Locked = True
                ActiveSheet.Cells(2, 8).Locked = True
            End If
    End Sub`

尝试使用此代码,但是请记住,除非工作表受到保护,否则锁定单元格不会产生任何影响 还要注意,将ByVal Target as Range用作Worksheet_Change参数的正确语法(而不是ByVal Target as Cells )。

Private Sub Worksheet_Change(ByVal Target As Range)

If Len(Target) Then

    Select Case Target.Column

        Case Is = 4

            Me.Unprotect "pwd"
            Cells(Target.Row, 8).Locked = True
            Cells(Target.Row, 9).Locked = True
            Me.Protect "pwd"

        Case Is = 8

            Me.Unprotect "pwd"
            Cells(Target.Row, 4).Locked = True
            Cells(Target.Row, 9).Locked = True
            Me.Protect "pwd"

        Case Is = 9

            Me.Unprotect "pwd"
            Cells(Target.Row, 4).Locked = True
            Cells(Target.Row, 8).Locked = True
            Me.Protect "pwd"

    End Select

End If

End Sub

暂无
暂无

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

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