ON ERROR RESUME NEXT

bschulze

Active Member
Joined
Jun 2, 2005
Messages
289
I have a macro that opens a file. If it does not find the file it END's the macro. This code works well on this macro however I created a "RUN ALL" macro and this code closed out of the entire process.

On Error Resume Next
'code to open file
If Err.Number <> 0 Then End
On Error GoTo 0

RUN ALL:

Application.Run("macro1")
Application.Run("macro2")


If the code in Macro1 does not find the file to open it stops but does not run Macro2.



Please help
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You need to put in error routines in the called macros as well. Something like:

Code:
Sub Macro1()

On Error GoTo ExitRoutine
'your code
Exit Sub

ExitRoutine:
End Sub

Hope this helps you out.
 
Upvote 0
I put that code in and it did the same thing. When I run the RUN ALL macro this part of the code closes out of the entire process. Please let me know if you notice something that is incorrect. My code now reads:

Sub Macro1()
'formating code
On Error GoTo ExitRoutine
On Error Resume Next
'open file code
If Err.Number <> 0 Then End
On Error GoTo 0
'formating code
Exit Sub
ExitRoutine:
End Sub



Thanks again
 
Upvote 0
The code is is working properly. It did not find the file to open so it ended the sub.

Sub RUN ALL ()
Application.Run("Macro1")
Application.Run("Macro2")
End Sub


The file name in macro1 is missing and it stops that process like it should. I want the RUN ALL macro to then execute macro2. However when it does not find the file in macro1 it never moves on to macro2. Should I be writing the RUN ALL macro differently?
 
Upvote 0
bschulze said:
The file name in macro1 is missing and it stops that process like it should. I want the RUN ALL macro to then execute macro2. However when it does not find the file in macro1 it never moves on to macro2. Should I be
writing the RUN ALL macro differently?

So would this work?
Code:
Sub Macro1() 
'formating code 
On Error GoTo ExitRoutine 
'open file code 
On Error GoTo 0 
'formating code 
Exit Sub 
ExitRoutine: 
End Sub 
[code]

I deleted the "On Error Resume Next" and "If Err.Number <> 0 Then End" lines.
 
Upvote 0
Is this what you are trying to do?

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> RunAll()
<SPAN style="color:#00007F">Call</SPAN> M1
<SPAN style="color:#00007F">Call</SPAN> M2
MsgBox "Done"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> M1()
<SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> NoFile
Workbooks.Open NoFileHere
<SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
<SPAN style="color:#007F00">'do stuff when the file exists and has opened</SPAN>
<SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
NoFile:
MsgBox "File not found in M1."
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> M2()
MsgBox "Into Sub 2"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0
I took those out and steped through the macro and it moves to macro2 now. Thanks for taking the time to explain a simple procedure to the beginner of all beginners


Thanks again
 
Upvote 0

Forum statistics

Threads
1,207,111
Messages
6,076,613
Members
446,216
Latest member
BEEALTAIR

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