简体   繁体   English

根据另一个单元格的值锁定某些单元格

[英]Lock certain cells depending on value from another cell

Im looking to lock a particular range of cells based on the input from a drop down in a row of data? 我是否希望根据一行数据的下拉菜单中的输入来锁定特定范围的单元格?

For example, In every row, third cell asks a question - "YES" or "NO" via a dropdown is given.If the user selects "NO", then remaining three cells has to be locked. 例如,在每一行中,第三个单元格会询问一个问题-通过下拉菜单给出“是”或“否”。如果用户选择“否”,则必须锁定其余三个单元格。 However, if the user selects, "Yes", then the remainder of the cells shall remain available to enter data into. 但是,如果用户选择“是”,则其余单元格将仍然可用于向其中输入数据。

   A         B          C            D         E         F  
| S.No   |  ID    |  Response   |  TIme  |  Value  | Expense | 
| 1      |  12345 |  NO         |  ----  |  ----   |  ----   |  (Locked Cells)
| 2      |  67y45 |  YES        |  44    |  1.68   |    1500 |  (UnLocked Cells)
| 3      |  12456 |  NO         |  ----  |  ----   |  ----   |  (Locked Cells)

like D2 to F2 has to be locked while D3 to F3 should be available for entry and again D4 to F4 is locked and so on,... 例如必须锁定D2至F2,而应该可以输入D3至F3,然后再次锁定D4至F4,依此类推,...

I tried the below code but it didn't lock the cells or didn't do any actions as far I observed 我尝试了以下代码,但据我观察,它没有锁定单元格或未执行任何操作

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("C2:C160") = "YES" Then
        Range("D2:D160").Locked = False
    ElseIf Range("C2:C160") = "NO" Then
        Range("D2:D160").Locked = True
        Range("E2:E160").Locked = True
        Range("F2:F160").Locked = True
    End If
End Sub

Took 160 as the highest range. 160为最高射程。

Any help would be really helpful, thanks. 任何帮助都会非常有帮助,谢谢。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("C2:C160")) Is Nothing Then Exit Sub
    If Target.Cells.Count > 1 Then Exit Sub

    Me.Unprotect Password:="password"

    If Target = "YES" Then            
        Target.Offset(0,1).Resize(1,3).Locked = False
    ElseIf Target = "NO" Then
        Target.Offset(0,1).Resize(1,3).Locked = True        
    End If

    Me.Protect Password:="password"

End Sub

The idea is to check the Target value. 这个想法是检查Target It is the cell, which is changed. 它是单元,已更改。 Thus, you can start with Intersect() , which checks whether the changed cell is in Range("C2:C160") : 因此,您可以从Intersect()开始,它检查更改后的单元格是否在Range("C2:C160")

If Not Intersect(Target, Range("C2:C160")) Then Exit Sub

Then try to see whether the changed cells are more than 1: 然后尝试查看更改后的单元格是否大于1:

If Target.Cells.Count > 1 Then Exit Sub

At the end, based on the "YES" and "NO" you can give lock cells. 最后,根据"YES"和“否”,您可以指定锁单元。 You may consider using UCase() , in order to make your code work for "yes" as well" 您可以考虑使用UCase() ,以便使代码对“是”也适用”

If UCase(Target) = "YES" Then
...
ElseIf UCase(Target) = "NO" Then

Concerning locked and unlocked cells and whether you can select them, see this article, the properties are explained quite well: 关于锁定和未锁定的单元格以及是否可以选择它们,请参阅本文,对属性进行了很好的解释:

https://support.office.com/en-us/article/lock-or-unlock-specific-areas-of-a-protected-worksheet-75481b72-db8a-4267-8c43-042a5f2cd93a https://support.office.com/en-us/article/lock-or-unlock-specific-areas-of-a-protected-worksheet-75481b72-db8a-4267-8c43-042a5f2cd93a

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

相关问题 锁定多个单元格,具体取决于另一个单元格中的数字 - Lock a number of cells depending on a number in another cell 根据另一个单元格的值锁定某些单元格 - Lock Certain Cells Based On Another Cells Value 电子表格:将一个单元格的值与另一个值进行比较(取决于另一个值) - Spreadsheet: compare a cells value to a certain range depending on another Value 可以根据另一个单元格的值锁定单元格范围吗? - Possible to Lock Range of cells based on another cell's value? 根据另一个单元格的值锁定和解锁单元格(excel 2013) - lock and unlock cells based on another cell's value (excel 2013) 如何根据另一个单元格的值列出单元格的内容 - How to list the content of cells depending on the value of another cell Excel VBA按特定顺序检查单元格,然后从另一个单元格中的其中一个返回值 - Excel VBA checks cells in certain order before returning a value from one of them in another cell 如果在另一个单元格中输入了某个值,是否有一个Excel公式填充多个单元格? - Is there an Excel formula that populates a number of cells, if a certain value is entered in another cell? 如何根据另一个单元格的值为单元格赋值? - How to assign value to cell depending on value from another cell? 如何根据另一个单元格的值锁定单元格 - How to lock a cell based off of a value from another cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM