根据相邻单元格的条件解锁单元格

发布于 2024-07-18 05:06:57 字数 443 浏览 5 评论 0原文

我有两列,但我想要的条件是从一个单元格到另一个单元格进行评估。

第一列的单元格具有名称的下拉验证,第二列仅在选择相邻单元格中的某个名称时才会激活。

到目前为止,我只找到了这段代码,但它似乎不起作用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼泪都笑了 2024-07-25 05:06:57

Target 参数告诉您正在更改的范围。

您需要执行如下操作:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range

    Set rng = Intersect(Range("A1:A10"), Target)
    If rng Is Nothing Then
        ' Not updating the range we care about
        Exit Sub
    Else
        rng.Offset(0, 1).Locked = ... whatever ...
    End If
End Sub

请注意,您的目标范围可以超过一个单元格(例如,使用复制/粘贴时),因此您需要处理和测试这种情况。

调用 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:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range

    Set rng = Intersect(Range("A1:A10"), Target)
    If rng Is Nothing Then
        ' Not updating the range we care about
        Exit Sub
    Else
        rng.Offset(0, 1).Locked = ... whatever ...
    End If
End Sub

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)

蓝颜夕 2024-07-25 05:06:57

该代码片段非常适合我。

您是否将该代码放置在正确的 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 a WorkSheet object for every sheet in your file. Double-click on one of these to edit the code for that WorkSheet. This is where your code snippet should go.

表情可笑 2024-07-25 05:06:57
Private Sub Worksheet_Change(ByVal Target As Range)

If Range("A1").Value = "Car" Then
    Range("B1").Locked = False
    Me.Unprotect ("password")
Else
    Range("B1").Locked = True
    Me.Protect ("password")
End If

End Sub

使用 Me.Protect 以便 .Locked 方法执行某些操作。 不过,您可能应该解锁所有其他单元格。

Private Sub Worksheet_Change(ByVal Target As Range)

If Range("A1").Value = "Car" Then
    Range("B1").Locked = False
    Me.Unprotect ("password")
Else
    Range("B1").Locked = True
    Me.Protect ("password")
End If

End Sub

Use Me.Protect so the .Locked method does something. You should probably unlock every other cell though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文