简体   繁体   English

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

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

I have a worksheet with many different columns for input. 我有一个工作表,其中有许多不同的列供输入。 The columns in question are D, H, and I. D and H are drop downs. 有问题的列是D,H和I。D和H是下拉列表。 I is any text input. 我是任何文字输入。 I am trying to put together a VBA script so that if a selection is made in D2 then that will lock H2 and I2. 我试图将VBA脚本放在一起,以便在D2中进行选择时将锁定H2和I2。 If a selection is made in H2 then that will lock D2 and I2. 如果在H2中进行了选择,则将锁定D2和I2。 If text is entered in I2 then that will lock D2 and H2. 如果在I2中输入文本,则将锁定D2和H2。 Lastly, this will need to be done for the entire column of D, H, and I so that each cell in those columns have the same property ie D16 and H16 will lock if I16 is filled and so on and so forth. 最后,将需要对D,H和I的整个列进行此操作,以使这些列中的每个单元格都具有相同的属性,即,如果I16被填充等等,D16和H16将锁定。

If this can be implemented by formulas as well I do not mind. 如果这也可以通过公式来实现,我不介意。

  `  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`

Try this code, but bear in mind locking cells as no affect unless the worksheet is protected. 尝试使用此代码,但是请记住,除非工作表受到保护,否则锁定单元格不会产生任何影响 Also note that ByVal Target as Range is the correct syntax for the Worksheet_Change argument (not ByVal Target as Cells ). 还要注意,将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.

相关问题 根据另一个单元格的值锁定和解锁单元格(excel 2013) - lock and unlock cells based on another cell's value (excel 2013) VBA Excel 根据单元格输入突出显示单元格 - VBA Excel Highlighting cells based on cell input 如何根据单元格值锁定 Excel 中的单元格行? - How Can You Lock Rows of Cells in Excel Based on Cell Value? excel-根据第一个单元格中的数字将单元格数据拆分为其他单元格 - excel - Splitting cell data into other cells based on a number in the first cell 根据其他2个单元格的值将excel单元格设置为强制性 - Make an excel cell mandatory, based on value of other 2 cells 基于Excel中其他单元格值组合的单元格返回值 - Return value in cell based on other cells values combination in excel 根据其他几个单元格 (VBA) 的值向 Excel 中的单元格添加注释 - Adding a note to a cell in Excel based on the values of several other cells (VBA) Excel-根据其他突出显示的单元格使用条件格式突出显示单元格 - Excel - Highlight a cell using conditional formatting based on other cells that are highlighted Excel VBA颜色单元格基于其他两个单元格中的值 - Excel VBA color cell based on value in two other cells Excel根据其他单元格值创建“条形”或填充一系列单元格 - Excel create “bars” or fill series of cells based on other cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM