根据相邻单元格的条件解锁单元格
我有两列,但我想要的条件是从一个单元格到另一个单元格进行评估。
第一列的单元格具有名称的下拉验证,第二列仅在选择相邻单元格中的某个名称时才会激活。
到目前为止,我只找到了这段代码,但它似乎不起作用:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1").Value = "Car" Then
Range("B1").Locked = False
Else
Range("B1").Locked = True
End If
End Sub
我需要这段代码来自(例如)A1:A10和B1:B10。
我希望我说得有道理。 如果有一种无需 VBA 即可完成的方法,那就太好了。
谢谢您的帮助。
I have two columns but the codition I would like is to be evaluated from one cell to another.
The first column has cells which have a drop down validation with names, and the second will activate only if a certain name from the adjacent cell is selected.
so far i only found this code but it does not seem to work:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1").Value = "Car" Then
Range("B1").Locked = False
Else
Range("B1").Locked = True
End If
End Sub
I would need this code go from (for example) A1:A10 and B1:B10.
I hope I am making sense. If there is a way to do it without VBA, that would be great.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Target 参数告诉您正在更改的范围。
您需要执行如下操作:
请注意,您的目标范围可以超过一个单元格(例如,使用复制/粘贴时),因此您需要处理和测试这种情况。
调用 Intersect 返回目标范围和您感兴趣测试的范围(本示例中的 A1:A10)的交集。
然后,您可以使用 .Offset(0,1) 访问相应的相邻单元格
The Target parameter tells you the range that is being changed.
You need to do something like the following:
Note that your target range can be more than one cell (e.g. when using copy/paste), so you need to handle and test this case.
Calling Intersect returns you the intersection of the target range and the range you are interested in testing (A1:A10 in this sample).
You can then access the corresponding adjacent cell(s) using .Offset(0,1)
该代码片段非常适合我。
您是否将该代码放置在正确的
WorkSheet
对象中? 如果只是将其放入 VBA 模块中是行不通的。 当您在 Visual Basic 编辑器中时,在屏幕左侧查找标有“Microsoft Excel 对象”的目录。 在该目录中,文件中的每个工作表都应该有一个WorkSheet
对象。 双击其中之一以编辑该WorkSheet
的代码。 这是您的代码片段应该所在的位置。That code snippet works perfectly for me.
Did you place that code in the proper
WorkSheet
object? It won't work if you just put it into a VBA module. When you are in the Visual Basic Editor, look for a directory on the left side of the screen labeled "Microsoft Excel Objects". In that directory should be aWorkSheet
object for every sheet in your file. Double-click on one of these to edit the code for thatWorkSheet
. This is where your code snippet should go.使用 Me.Protect 以便 .Locked 方法执行某些操作。 不过,您可能应该解锁所有其他单元格。
Use Me.Protect so the .Locked method does something. You should probably unlock every other cell though.