[英]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.