Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Have "ON ERROR" in a For-Next Loop, but still get errors when it's ok 1

Status
Not open for further replies.

JC10001

Vendor
Joined
Sep 17, 2003
Messages
6
Location
US
I wrote the following piece of code to check if all the names excluding Not Available in the Weights sheet (which are in a column starting from P6) have a match in the Report sheet. If it doesn't, it goes to the errorHandler like I intended. But, if there aren't any errors, it still gives me the error message with the last name.

I see why I get the error message with the last name. Is there any way around this?

Sub CheckName
Dim Name as String, Number as Integer, i as Integer
Number = Range("p4")
Range("p6").Select
For i = 1 To Number
Sheets("Weights").Select
If ActiveCell = "Not Available" Then
ActiveCell.Offset(1, 0).Range("A1").Select

Else: Name = ActiveCell
ActiveCell.Offset(1, 0).Range("A1").Select
Sheets("Report").Select
Cells.Find(What:=Name, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
On Error GoTo errorHandler
End If
Next i
errorHandler: MsgBox (Name & " not found in Report.")
End Sub
 
Hi,

You need :

Exit Sub
errorHandler: MsgBox (Name & " not found in Report.")
End Sub

Jean-Paul
Montreal
jp@solutionsvba.com
mtljp2@sympatico.ca
 
Thank you Jean-Paul! Worked like a charm!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top