tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
In the sub below, should the On Error GoTo 0 be placed immediately after the line that might cause an error or should it be placed at the end, or indeed in both places?

Code:
    On Error GoTo Errhandler

        Worksheets("Sheet10").Activate

    'On Error GoTo 0 'SHOULD THIS BE PLACED HERE???????
    
    Range("A1").Value = 123
    
    GoTo Anothersub
    
Errhandler:
    
    Worksheets.Add.Name = "Sheet10"
    
Anothersub:
    
    On Error GoTo 0
    
    Call SomeModule.SomeSub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Neither, really. You should use On Error Resume Next to assign the sheet to a variable, test if it's Nothing and add the sheet if so (you could put that into a separate function). Then On Error Goto your error handler, if you have one. You should not be jumping about all over the place with Goto statements.
 
Upvote 0
I was following this:

Code:
https://www.wiseowl.co.uk/videos/excelvba/error-handling.htm

So is the video displaying bad practice?
 
Upvote 0
If that's what it shows, then yes, in my opinion.
 
Upvote 0
So you suggest the following?

Code:
    If Not (FnCheckWorksheetName.CheckWorksheetName(wksname:="Sheet10")) Then Worksheets.Add.Name = "Sheet10"
        
    Range("A1").Value = 123<strike></strike>
  
    Call SomeModule.SomeSub

The function:

Code:
Public Function CheckWorksheetName(ByRef wksname As String) As Boolean
    
    Dim wks As Worksheet
 
    On Error Resume Next
    
        Set wks = ThisWorkbook.Worksheets(wksname)
    
    On Error GoTo 0
    
    CheckWorksheetName = Not wks Is Nothing
    
    Set wks = Nothing
    
End Function
 
Last edited:
Upvote 0
Something along those lines, yes.
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,840
Members
449,193
Latest member
MikeVol

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