Handling Multiple Errors within one loop

SlinkRN

Well-known Member
Joined
Oct 29, 2002
Messages
724
I am trying to work with a loop so that if an error occurs when searching for a name (name is not found in list), it will then skip some of the lines of the loop and continue on further down in the loop. Unfortunately, it works for the first error but then not again. I simplified the loop as much as I could to keep the code here in the post to a minimum. I cannot use Resume Next because if the name is not found I don't want to enter scores anywhere. Where can I put code that will reset the On Error part so it will work again the next time a name isn't found. I searched the forum but I just can't get it to work. Thanks! Slink

VBA Code:
Sub simpleloopwitherrors()
Dim i As Integer
Dim Bowler As String
Dim ans As Integer
Bowler = "Start"
i = 1
Do Until Bowler = ""
    Bowler = Sheets("Bowler").Range("A" & i)
    Sheets("Scores").Select
    Range("A1:A10").Select
    On Error GoTo noname
        Selection.Find(What:=Bowler, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    ActiveCell.Offset(0, 2).Value = 100
    ActiveCell.Offset(0, 3).Value = 200
    ActiveCell.Offset(0, 4).Value = 300
    GoTo nextbowler:  'this skips the noname error code
noname:
ans = MsgBox("The name " & Bowler & " does not exist (or is misspelled). Would you like to skip this bowler?", vbYesNo, "Name not found")
    If ans = vbYes Then
          GoTo nextbowler
        Else
    Exit Sub
    End If
nextbowler:

i = i + 1

Loop
   
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I don't think you should be using error handling in this case to manage your logic, because failure to find something is not really an error, it's a normal condition that you should expect could happen. I also recommend against using selection, activation.

Honestly I'm not clear on your question. You are exiting the sub if the user answers "No", so I'm not clear on what it is you want to do. But here is a rewrite of your code. If this doesn't solve your problem then it should make it more straightfoward to solve.

VBA Code:
Sub simpleloopwitherrors()

   Dim i As Integer
   Dim Bowler As String
   Dim ans As Integer
   Dim Found As Range
  
   Bowler = "Start"
   i = 1
  
   Bowler = Sheets("Bowler").Range("A" & i)
  
   Do Until Bowler = ""
      
       Set Found = Sheets("Scores").Range("A1:A10").Find(What:=Bowler, After:=Found, LookIn:=xlFormulas, LookAt _
           :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
           False, SearchFormat:=False)
          
      If Found Is Nothing Then
         ans = MsgBox("The name " & Bowler & " does not exist (or is misspelled). Would you like to skip this bowler?", vbYesNo, "Name not found")
         If ans <> vbYes Then
            Exit Sub
         End If
      Else
         Found.Offset(0, 2).Value = 100
         Found.Offset(0, 3).Value = 200
         Found.Offset(0, 4).Value = 300
      End If
  
      i = i + 1
      Bowler = Sheets("Bowler").Range("A" & i)
  
   Loop
  
End Sub
 
Upvote 0
Solution
Thanks so much for your reply! This sounds exactly like what I was looking for! Unfortunately, I'm getting a Run-time error '13': Type mismatch when it gets to the Set Found line. Here is my actual range for this section. Does it change things that my actual range is bigger than my sample code?
VBA Code:
Set Found = Sheets("Team_" & TeamNumber).Range("A3:AG55").Find(What:=Bowler, After:=Found, LookIn:=xlFormulas, LookAt _
        :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)
 
Upvote 0
Yay! That got rid of the error, just working on getting the rest of my statements fixed up so it does what I need it to do. I'll let you know if I run into any other errors! Thanks so much! The Found statement really helps!!
 
Upvote 0
Try removing this "After:=Found,"

Try removing this
"After:=Found,"
Hi again Alex and Jeff! I love the using the Found option but I'm wondering if I can get the cell address of where the name was found. Right now, the value of Found is the bowler's name and I need to know exactly where that bowler's name is on the sheet. Is there an easy way to get the cell address as the value of Found? Thanks, Slink
 
Upvote 0
"Found" is a range > Dim Found As Range
So Found.Address gives you the address.

You can also use it like this:
VBA Code:
    Found.Offset(0, 2).Value = 100
    Found.Offset(0, 3).Value = 200
    Found.Offset(0, 4).Value = 300
 
Upvote 1
Ah, I see that now. Jeff did give me that code but when I was having loop errors I put the statement msgbox = Found and it showed it as a name so I thought that was the problem. I see now that it will work as a range. I just need to keep working on my loops until the whole thing works. Thanks again for your help!
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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