根据其他单元格输入

时间:2015-11-16 16:28:51

标签: excel vba excel-vba

我有一个包含许多不同列的输入工作表。所讨论的列是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`

1 个答案:

答案 0 :(得分:0)

尝试使用此代码,但请记住将单元格锁定为无影响,除非工作表受到保护。另请注意,ByVal Target as RangeWorksheet_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
相关问题