Exit sub but not working

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
778
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have three pieces of code that do various stuff and are called from a button and run one after another. My problem lies in the first code. The code checks whether a file exists. If it does all is well. If it doesn't, I have used Exit Sub. But after it uses the Exit Sub the code jumps to the next piece of code and cause an error.

How do I get the code to completely stop at Exit Sub?
 
Until they add Try...Catch...Finally to VBA, I'm not sure what else you would use?
No hablo C. Is that like posit...quit?

I like to detect errors before they happen rather than let them happen and then jump around like a demented kangaroo. If I do use On Error - and that's not very often - it's always with Resume Next - never with GoTo (which is not required in a block-structured language and should trigger a warning message whenever it's used) - and it's almost always reset after the immediately following statement.

On Error directives which endure for large sections of code where you're trying to trap some entirely foreseeable condition can mask errors which you weren't expecting and this can lead to erroneous results.

Why am I telling you stuff you already know? :)

The conversation might give someone else food for thought.
 
Last edited:
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
It's the .net error handler syntax.

I do use inline On Error Resume Next (which is still On Error, no? ;)) statements but they generally tend to occur in specific functions where the error is expected to some degree (SheetExists type functions) and they are the only error handling in a short function.

I think all other routines should have an on error goto handler type statement simply because you cannot foresee everything, and it allows you to log things, report something useful to the user and then perform any necessary cleanup before exiting if appropriate.

As with all good rules, there are exceptions and I totally agree that it is better to prevent the error than to handle it (however that may be). Trying to use the result of a Find without testing for nothing is the classic misuse (to my mind) of an error handler. I also prefer Application.function to Application.worksheetfunction.function for similar reasons, though I know others disagree.

Whichever way you choose to go, consistency is key (as with most things related to programming). :)
 
Upvote 0
I also prefer Application.function to Application.worksheetfunction.function
Ditto, sort of. Every object in the Excel OM belongs to the Application (e.g., WorksheetFunction, ActiveWorkbook, ActiveSheet, ...), and I just ain't gonna type Application that many times when it's not required, so I use WorksheetFunction.func (for IntelliSense), and Application.func when I feel an error comin' on.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,734
Members
452,939
Latest member
WCrawford

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