Christian J
Christian J

Reputation: 41

VBA: Message Box Display, Loop on Condition

I have created VBA code that displays a message box when a certain condition is met. This if statement loops through a determined range. However, the message box is displayed for every cell that meets my condition. Is there a way to ensure the message box is only displayed once?

Here is my code:

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

Upvotes: 0

Views: 2231

Answers (1)

DisplayName
DisplayName

Reputation: 13386

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

Upvotes: 2

Related Questions