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

Error in 1st Procedure of multiple calls

Status
Not open for further replies.

jdttek

Technical User
May 8, 2002
112
US
I am just learning proper error handling and need some help on proper etiquite.
I have module with 4 sub procedures that:
Sub Procedure 1 - Runs Sub Procedures 2-4.
Sub Procedure 2 - Open workbook
Sub Procedure 3 - Parses and formats file
Sub Procedure 4. Saves file


I have built On Error in Sub Procedure 2 to notify user if workbook does not exist, and then end the Sub Procedure. It does this fine, but then, since I start with Procedure 1, it tries to continiue with 3 & 4. How do I struture the On Error in Procedure 1 so that, if the workbook does not exist in Procedure 2, it does not even try to coninue with 3&4?

Thanks
jdttek
 
One way that is often done is to write procedures 2 through 4 as
function xxxxx() as boolean
xxxxx=true
on error goto ErrorHandler
....
exit sub
ErrorHandler:
xxxxx=false
end sub

Now your procedure 1 can decide, based on the status of procedures 2..4, how to proceed.
Rob
[flowerface]
 
Thanks.

So I leave Procedure 1 as Sub and cbange 2-4 to Function?
 
That's what I suggested, yes. Of course there's more than one way to skin this cat.
Rob
[flowerface]
 
Though it's a while since I've done this I believe Rob's suggestion is the 'better' way to do things but here's the other way to skin the cat. Beware that this can get quite confusing, or was that just me.

Code:
Sub a()
On Error GoTo mash
Call b
Call c
mash:
MsgBox "Error " & Err & " " & Err.Description
End Sub

Sub b()
Workbooks.Open "C:/my documents/my book.xls"
End Sub

Sub c()
'pointless sub here as processing shouldn't get this far
MsgBox "How Did I Get Here?"
End Sub

;-) If a man says something and there are no women there to hear him, is he still wrong?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top