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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On error only works once ??? 1

Status
Not open for further replies.

MikeCDPQ

Technical User
Sep 11, 2003
173
CA
Here is a short script that I am having a problem with.

User must input PO number. If PO not found ON ERROR clause works first time. If user re-enters valid PO, no problem. However if user enters another PO that can't be found, then I get error: Run Time error 91: Object variable or with block variable not set.

Why would it capture the first error but not consecutive ones ?

Here's the script:




Sub FindPO()

Dim POSEARCH

POSEARCH = InputBox("Enter PO number to find.")
If POSEARCH = "" Then Exit Sub

Restart:

On Error GoTo POnotFound

Columns("F:F").Select

Selection.Find(What:=POSEARCHNEW, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

Exit Sub

POnotFound:

POSEARCH = InputBox("PO number: " & POSEARCH & " was not found. Please enter valid PO number !")

GoTo Restart

End Sub


Thanks for any suggestion

 
1st - Where's the END IF?

2nd - Goto Restart - How does user input again?
 
There is no IF condition other then the one liner that doesn't need an endif (This will simply cancel the process if user did not enter any value in the inputbox).

If you look at the script the "on error" will trigger an inputbox by the same name and user would enter another PO which is then searched by starting over at the restart position.

Unless, I'm not understanding your question.

Thanks.
 
WZUP, one line IF statements do not have End If:
Code:
If POSEARCH = "" Then Exit Sub
     [green]'is the same as[/green]
If POSEARCH = "" Then 
    Exit Sub
End If
2nd - look at the line above
GoTo Restart
that's where it is.

Have fun.

---- Andy
 
Aside from the educational exchanges :) does anyone have any idea of how to fix my error message ?

Thanks for any suggestions.

Mike
 
Two possible methods:

1. Instead of "GoTo Restart" use "Resume Restart"
or
2. Just before "GoTo Restart", add an Err.Clear call
 
Hi SBerthold,

Using Resume instead of Goto did the trick and worked like a charm :)

Thanks a whole bunch.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top