Below code gives Msg box for every instance when condition is met in a range. Need it to give Msg box only once, even if multiple lines in range meet the condition
Exit For Next once condition is met
-
acat -
April 16, 2019 at 4:08 PM -
Thread is marked as Resolved.
-
-
-
Try:
Code
Display MoreSub TestIfNoEmail() Dim x As Long, lRow As Long, cnt As Long With Sheet1 lRow = .Range("D4").Value For x = 6 To lRow If Not IsEmpty(.Cells(x, 3)) And IsEmpty(.Cells(x, 5)) Or .Cells(x, 5) = 0 Then cnt = cnt + 1 End If Next x End With If cnt > 0 Then MsgBox "Need to add code which will give a Message Box" End If End Sub
-
Hi,
The clue is in the title of yoru post
Code
Display MoreSub TestIfNoEmail() Dim x As Long Dim lRow As Long With Sheet1 lRow = .Range("D4").Value For x = 6 To lRow If Not IsEmpty(.Cells(x, 3)) And IsEmpty(.Cells(x, 5)) Or .Cells(x, 5) = 0 Then MsgBox "Need to add code which will give a Message Box" ' Exit For ' 'Exit the For Next loop and continue immediately after the Next statement ' End If Next x End With End Sub
You could also just end the count by adding x=lRow instead of Exit For.
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!