On Error

jebenexcel

Board Regular
Joined
Mar 16, 2018
Messages
59
Hello,

I have a macro that might refer to nonexisting sheets. I want the macro to, let's say, select the A1 Cell on the sheet a if an error occurs.
How do I implement this with On Error Go To?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Avoid On Error Goto unless it's to exit the VBA cleanly altogether. You're better having a Sub you can call.

Code:
On Error Resume Next
...... refer to non-existing sheet
If Err.Number <> 0 Then
     On Error Goto 0
     Call ErrorCode
End If
On Error Goto 0
... rest of code

Code:
Sub ErrorCode()
Sheets("a").Activate
Range("A1").Select
End Sub
 
Upvote 0
The problem with On Error Goto x is that VBA doesn't know where the Goto was called from, so you would need to use a Goto back to the statement after the error. Which is fine if it's one macro and the error can only occur in one place. But if you subsequently add another bit of code where the error might occur, you need to start using flags to tell the error code where to go back to. It gets messy. it's simpler to just have a Sub which automatically returns to where it was called from.

The issue doesn't arise if you are just cleanly exitting a sub after an error, but it's a good habit not to use On Error Goto with anything other than 0. Even if you want to exit after a fatal error, you can use
Code:
On Error resume Next
...
If Err.Number <> 0 Then
     Call ErrorHandler
     Exit Sub
End If
On Error Goto 0

I studied Comp Sci at university, it was a mandatory fail on any assignment in any language if you used GoTo - a lesson I learnt very quickly :mad: and I've never used a Goto since (30 years cough cough)
 
Last edited:
Upvote 0
The problem with On Error Goto x is that VBA doesn't know where the Goto was called from, so you would need to use a Goto back to the statement after the error. Which is fine if it's one macro and the error can only occur in one place. But if you subsequently add another bit of code where the error might occur, you need to start using flags to tell the error code where to go back to. It gets messy.

Not really - either Resume or Resume Next should suffice.
 
Upvote 0
I just solved my problem using If clauses ( referring to the existing sheet ordered by number only if they exist) :)
 
Upvote 0
That's alwways the best solution - avoiding the chance of errors in the first place :)
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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