[ on error go to ] function

kraaniks

New Member
Joined
Sep 19, 2016
Messages
26
Dear excel users,

Is this syntax correct ? I am getting debugging error second time error comes, however it ignores the error for the first time. I want it to skip all errors not just to stop at second.

I have not used exit sub or on error go to 0 statements.


Code:
For var_country = 1 To 4
For var_functions = 1 To 2

Functions(1) = "AP"
Functions(2) = "AR"


country(1) = "NO-"
country(2) = "SE-"
country(3) = "DK-"
country(4) = "PL-"

result = country(var_country) & Functions(var_functions)

On Error GoTo Errorhandler:


'MY CODE HERE

Errorhandler: 

Next var_functions


Next var_country




End Sub
 
Last edited:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Why haven't you used On Error Goto 0?

That 'resets' the error handler, which is what you probably want to do.

By the way, what error(s) are you trying to handle?
 
Upvote 0
You need to clear an error after you've trapped it.

Code:
On Error Goto To Handler

For var_functions = 1 To 4
    For var_country = 1 to 2
        ' code

Handler:
    Err.Clear
    Next var_country
Next var_functions

On Error Goto 0
 
Upvote 0
I have a cells.find(what:=result) function which searches for "result" based on above code in a sheet, if it doesnt find the value for the second time it gives me debugging, it ignores first time it appears.

So I need to use on error go to 0 somewhere?
 
Upvote 0
You usually also want to put an "Exit Sub" row just before the Errorhandler code.
That way, if your procedure runs through all the way without finding any errors, it exits the code before hitting the error handling code at the end.
 
Upvote 0
Rather than using On Error, you can test if if .Finds nothing


Code:
for var_country = 1 to 2
    ' code    

    Set myCell = .Find(want:= result)

    If myCell Is Nothing Then

       ' process my cell
    Else
Next var_country
 
Upvote 0
You don't need error handling for that. Assign the result of the Find operation to a Range variable then test if it Is Nothing before you try and access any of its properties.

As regards your specific question about error handling, neither Err.Clear nor On Error Goto 0 will actually clear the current exception state - you should use a Resume statement for that. See here.
 
Last edited:
Upvote 0
I want to continue with "next" in both instances, if there is an error, by skipping the core code, and if there is no error then run through all the code. So then it would look like this: IF my cell is nothing then next, Else code, and then next. I dont think thats possible.

And clearing error with err.clear or on error goto 0 doesnt work.
 
Last edited:
Upvote 0
You just put the bulk of you code inside the "found cell is not Nothing" branch of an if


Code:
For num_County = 1 to 2

    Set myCell = someRange.Find(what:="something")

    If myCell is Nothing Then
        ' do nothing
    Else
        ' code to do something
    End If
Next num_country
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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