Search code examples
vbaloopsconditional-statementsmessagebox

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

Solution

  • 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