VBA:消息框显示,循环条件

时间:2018-03-13 20:00:32

标签: vba loops conditional messagebox

我创建了VBA代码,在满足特定条件时显示消息框。此if语句循环到确定的范围。但是,将为符合我条件的每个单元格显示消息框。有没有办法确保消息框只显示一次?

这是我的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
For Each cell In Range("S194:BZ194")
    If cell.Value < 0 Then
MsgBox "Unrestricted cash cannot be less than zero (Row 194). Please lower the loan growth rate."
End If
Next
End Sub

1 个答案:

答案 0 :(得分:2)

Private Sub Worksheet_Change(ByVal Target As Range)
    For Each cell In Range("S194:BZ194")
        If cell.Value < 0 Then
            MsgBox "Unrestricted cash cannot be less than zero (Row 194). Please lower the loan growth rate."
            Exit For
        End If
    Next
End Sub