Make For Loop Show Error Message

marsow

New Member
Joined
Jan 30, 2015
Messages
28
Hi everyone,


I can't figure out how to make my simple for loop code show an error message when it no longer finds any cell(s) that meet the criteria. The criteria is that the first cell must match "K1110" while 4 cells to it's right must be blank. Please note that this code works fine. But, how can I make it say "nothing found" if it found nothing? And make it say "only 2 were done" out of X?


I tried to search for a solution but with not luck. Please help.


For X = 1 To 3
For Each cel In SRng1
If cel = Range("K1110") And cel.Offset(0, 4) = "" Then

cel.Offset(0, 3) = Range("L1110")
cel.Offset(0, 4) = Range("M1110")
Exit For
End If
Next cel
Next X

==========


All the best,
-m
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Maybe
Code:
Dim Flg As Boolean, Cnt As Long
For x = 1 To 3
   For Each Cel In SRng1
      If Cel = Range("K1110") And Cel.Offset(0, 4) = "" Then
         Cel.Offset(0, 3) = Range("L1110")
         Cel.Offset(0, 4) = Range("M1110")
         Flg = True
         Cnt = Cnt + 1
         Exit For
      End If
   Next Cel
Next x
If Flg Then MsgBox Cnt & " Where done" Else MsgBox "None found"
 
Upvote 0
Hi again @Fluff.
I hope it's okay, is it possible to modify this a little so that it won't say anything if all X is completed?
Thank you! :)

Maybe
Code:
Dim Flg As Boolean, Cnt As Long
For x = 1 To 3
   For Each Cel In SRng1
      If Cel = Range("K1110") And Cel.Offset(0, 4) = "" Then
         Cel.Offset(0, 3) = Range("L1110")
         Cel.Offset(0, 4) = Range("M1110")
         Flg = True
         Cnt = Cnt + 1
         Exit For
      End If
   Next Cel
Next x
If Flg Then MsgBox Cnt & " Where done" Else MsgBox "None found"
 
Upvote 0
Change the last line to
Code:
If Flg And Cnt < x - 1 Then
   MsgBox Cnt & " Where done"
ElseIf Not Flg Then
   MsgBox "None found"
End If
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top