Exit Macro on Error

muay74

New Member
Joined
Jul 29, 2011
Messages
2
I'm trying to make a routine stop if an error occurs but can't work out the code to make it stop the routine as if I exit the sub it simply goes back to the parent sub that called the routine ;<

Sure it's an easy thing just can't work out the code ;<
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Maybe like this

Code:
On Error GoTo KillMe
'
'your code here
'
Exit Sub
KillMe:
End
End Sub
 
Upvote 0
Or...
Code:
On Error GoTo KillMe
'
'your code here
'
Exit Sub

KillMe:
End Sub
 
Upvote 0
TBH I think the second offering will merely return control to the calling routine rather than end VBA completely.

One technique I often use is to make the called routine into a function - this isn't always possible - and return a value from the function indicating whether an error occurred or not: True/False for success/fail or the actual Err.Number (0 for success, etc). The calling routine is then responsible for testing this value and take the necessary action. So if the subroutine is called MyOtherCode, you'd call it like this:-
Code:
If MyOtherCode([I]parameters[/I]) <> 0 Then
  MsgBox "Argh!"
  Exit Sub
End If
I prefer to restrict code which does things as drastic as stopping the run to the main routine if possible, then this sort of problem doesn't crop up.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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