Exit sub only and only if there is an error in the procedure.

H

Heera

Hi,

I want a code which will exit the macro(Procedure) only and only if
there an error in the procedure.

Here is the code which i wrote but after the error message it
continues to run the next procedure and i want it to it to exit. If
there is no error it comes on the exit sub and stops the macro.

Sub TLPColl()

On error goto Jumpexit

workbook.open "XXXX"

Jumpexit:
Msgbox "Kindly mention the path where the raw file is saved in Cell D5
of Summary Tab"

Exit sub

'here are my procedure

Exit Sub

Kindly help.

Regards

Heera
 
P

Per Jessen

Hi

I would rather check if the file exists than rely on an error.

Look at this:

Sub TLPColl()

Set fs = CreateObject("Scripting.FileSystemObject")

a = fs.Fileexists(MyFile)
If a = False Then
' File don't exists in current folder
MsgBox "Kindly mention the path where the raw file is saved in Cell D5
of Summary Tab"
Exit Sub
Exit Sub

Workbook.Open Filename:="XXXX.xls"

'here are my procedure

End Sub

Regards,
Per
 
J

JLGWhiz

I think I would put it in this order. that way, if it goes to Jumpexit, it
is unlikelty to run the main code.

Sub TLPColl()

On error goto Jumpexit

workbook.open "XXXX"


'here are my procedure

Jumpexit:
Msgbox "Kindly mention the path where the raw file is saved in Cell D5
of Summary Tab"

Exit sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top