如何用超过2 If语句锁定范围

时间:2016-11-25 07:40:02

标签: excel-vba vba excel

是否锁定单元格超过2“IF”语句。

示例:如果1100000~1149999之间的值,则我要检查每一行(“L12:L48”),然后使用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一直出现,直到到达最后一行检查行(b'coz我在已检查的行上使用累积计算)并且单元格锁定不起作用。请打开下面的图片链接以获取Excel视图。

这可以通过VBA来实现吗?如果可以,怎么做?

非常感谢。

Locked Excel

2 个答案:

答案 0 :(得分: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

解决您的问题?

答案 1 :(得分:0)

Private Sub Worksheet_Change(ByVal Target As Range)

Dim i As Integer 静态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

End Sub

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