【问题标题】:How to Lock Range with more than 2 If Statement如何使用超过 2 个 If 语句锁定范围
【发布时间】:2016-11-25 18:09:00
【问题描述】:

是否是具有超过 2 个“IF”语句的 Lock 单元格。

示例:我想检查每一行 ("L12:L48") 是否存在 1100000 ~ 1149999 之间的值,然后使用MsgBox 。如果值大于 1150000 则使用 MsgBox 并锁定其他单元格 ("E12:E28")。

Private Sub Worksheet_Change(ByVal Target As Range)

Dim i As Integer

i = 12

Do
    If Cells(i, 12).Value >= 1100000 And Cells(i, 12).Value <= 1149999 Then
        MsgBox "Caution : Cutter Meter Nearly Exceed Limit"
    ElseIf Cells(i, 12).Value >= 1150000 Then
        MsgBox "Please Change Cutter"
    ElseIf Cells(i, 12).Value >= 1150000 Then
        ActiveSheet.Unprotect
        Range("E12:E48").Locked = True
        ActiveSheet.Protect
    End If
i = i + 1
Loop Until i = 48

End Sub

我已经运行了这段代码,但 MsgBox 一直出现,直到到达最后一个检查行(因为我在检查行上使用累积计算)并且单元格锁定不起作用。请打开下面的图片链接查看excel。

这可以使用 VBA 实现吗?如果可以,如何实现?

非常感谢。

Locked Excel

【问题讨论】:

  • 你为什么不把2个条件合并成1个条件?

标签: excel vba


【解决方案1】:

不会改变

来自

If Cells(i, 12).Value >= 1100000 And Cells(i, 12).Value <= 1149999 Then
    MsgBox "Caution : Cutter Meter Nearly Exceed Limit"
ElseIf Cells(i, 12).Value >= 1150000 Then
    MsgBox "Please Change Cutter"
ElseIf Cells(i, 12).Value >= 1150000 Then
    ActiveSheet.Unprotect
    Range("E12:E48").Locked = True
    ActiveSheet.Protect
End If

If Cells(i, 12).Value >= 1100000 And Cells(i, 12).Value <= 1149999 Then
    MsgBox "Caution : Cutter Meter Nearly Exceed Limit"
ElseIf Cells(i, 12).Value >= 1150000 Then
    MsgBox "Please Change Cutter"
    ActiveSheet.Unprotect

    If Range("E12:E48").MergeCells = False Then
        Range("E12:E48").Locked = True
    Else
        Range("E12:E48").MergeArea.Locked = True
    End If

    ActiveSheet.Protect
End If

解决您的问题?

【讨论】:

  • 在 "Range("E12:E48")Locked.True 打开此网址时仍然出现错误:imgur.com/8o2batq 谢谢...
  • 你试过ActiveSheet.Range("E12:E48").Cells.Locked = True吗?
  • 具体错误是什么?在这里和我聊天:chat.stackoverflow.com/rooms/129008/…
  • Eduard Daduya - 是的列 En k 已合并...稍后我会检查您附加的链接...谢谢...
【解决方案2】:

Private Sub Worksheet_Change(ByVal Target As Range)

将 i 调暗为整数 静态 bWarning 为布尔值

i = 12

Do
    If bWarning = False Then
        If Cells(i, 12).Value >= 1100000 And Cells(i, 12).Value <= 1149999 Then
            MsgBox "Caution : Cutter Meter Nearly Exceed Limit"
            bWarning = True
        ElseIf Cells(i, 12).Value >= 1150000 Then
            MsgBox "Please Change Cutter"
            bWarning = True
            ActiveSheet.Unprotect
            Range("E12:K48").Locked = True
            ActiveSheet.Protect
        End If
    End If
i = i + 1
Loop Until i = 48

结束子

*好的,伙计们...我已经解决了问题...他们两个... 1)锁定合并单元格中的区域(2)从多个弹出窗口中停止 msgBox... *非常感谢您的快速回复...非常感谢... :)

【讨论】:

    猜你喜欢
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    相关资源
    最近更新 更多