VBA: How long does On Error Resume Next work?

I am reading how to use On Error Resume Next , and I am trying to figure out how long this line will be applied to the program. I found this sentence on the Microsoft website: "The On Error Resume Next operation becomes inactive when another procedure is called." What exactly does this mean? What is considered a procedure?

I ask because I use a line in my program, but I do not want her Resume Next execute all runtime errors, only obvious on the next line.


Code: Dim zRange As Range

 Call FilterTableFor(fieldNameColumn, Array("baseunitprice", "burden", "MTLBURRATE", "PurPoint", "Vendornum")) On Error Resume Next Set zRange = commentsColumnRange.SpecialCells(xlCellTypeVisible) zRange.Formula = "target" Call FilterTableFor(fieldNameColumn) 

I also found (and have known for some time) that On Error or GoTo strings are considered bad encoding. Is there a Try-Catch that I can use for such a string?

I think something like this:

 Dim zRange As Range Call FilterTableFor(fieldNameColumn, Array("baseunitprice", "burden", "MTLBURRATE", "PurPoint", "Vendornum")) Try Set zRange = commentsColumnRange.SpecialCells(xlCellTypeVisible) zRange.Formula = "target" Catch() Call FilterTableFor(fieldNameColumn) 

Where I don’t even do anything with it, because I don’t feel the need.

Thank you for your time.

+5
source share
4 answers

Do you want to use "Enable error retry" if

  • You know the cause of the error.

  • You know that this will not affect other parts of the code.

  • You use "On Error Goto 0" immediately after the code in which the error occurs.

Having said that, you will almost never use it. You must find out why the error occurs and the code to handle it.

What the website says is that after you exit the subset or function that called it, the next resume will no longer affect you, and your errors will increase as they should.

A better alternative is to use goto in this way. But some people frowned almost the same way.

 sub SomeSub() On Error Goto TestFailed 'Some code 'Some code 'Some code Exit sub TestFailed: 'Some code here to alert you to and/or handle the fallout of the error. End sub 
+2
source

AREA <<20> STATEMENT

The ON ERROR ... effect ends as soon as one of the following events occurs:

  • Another ON ERROR ... (Possibly in the form of ON ERROR RESUME x or ON ERROR GOTO x )
  • Exit Sub / Exit Function within the same sub / function function as defined.
  • End Sub / End Function the sub / function function that is defined.

DO I NEED TO USE ON ERROR RESUME NEXT ?

Yes and No.

I would say do not use without knowing what the effect of this statement will be. Avoid if possible. Limit your scope if this is not possible.

To hide the action of the ON ERROR RESUME NEXT statement, you can call ON ERROR GOTO 0

+5
source

You don't always need a bunch of code to handle the error, but you really have to do something with it. Maybe just change your code on the cells.font.color property to vbRed. Just doing On Error Resume Next (a line of code that might error) On Error Goto 0 is a terribly bad form.

And, as others have noted, On Error Goto Label is essentially a version of VBA Try ... Catch , and I often use

0
source

To answer your question, "How long does the Error Inclusion feature work?"

Answer: until the next definition On error ...

So, if you define On error resume next , it will skip every error until you define On error goto 0 or On error goto label

0
source

All Articles