Can a sub have more than one OnError Statement???

AlexB123

Board Regular
Joined
Dec 19, 2014
Messages
207
Hi all,

I have an odd macro that makes use of OnError Resume Next ... my understanding is that this statement returns control of the sub to the following lines. In addition to this weird code block (I am working on a different approach, generally, but please bare with me), I want to insert a general error handler to exit the sub if a user runs into an error.

Is there a problem with having a general Error Handling label and statement, plus the second On Error Resume Next?

My feeling is that one will override the other and cause more problems ... but I'm not sure. Also, is the structure of the label statement OK?

Thanks!

Code:
Sub Register()
 On Error GoTo ErrHandl

Dim Success As Boolean
Dim excelfile As Variant
Dim ucrWB As Workbook
Dim ucrSh1 As Worksheet
Dim ucrPath As String


'do code

Do While excelfile <> ""
        
        Success = ""

        On Error Resume Next
        Set ucrWB = Workbooks.Open(ucrPath & excelfile)
            Set ucrSh1 = ucrWB.Sheets(1)
        Success = (Err.Number = 0)

            If Success = True Then
                            'Do More Stuff

           Else:
                            'Do other stuff
           End If

     excelfile = Dir
Loop

'Finish Code

           Exit Sub

ErrHandl:
MsgBox "There was an error."

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
You can only have one error handler active at a time, but you can have as many handlers as you want. In your example, you start with the ErrHandl: handler, but as soon as you hit the On Error Resume Next, that takes over. But if you want, you can put another On Error GoTo ErrHandl: line 3 lines later, and that takes over again. If you have a large subroutine, with many sections, you can set one for each section.

Your label is coded fine.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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