Error Handling Problem

Zabman

Board Regular
Joined
Apr 7, 2010
Messages
77
Hi,

I have the following errorhandling procedure, which is located at the end of my procedure.

When the code reaches this, even though I have already set errorhandling back to default (on error goto 0), it is going through this code even though there is no error.

Have I written this incorrectly, or put it in the wrong place? I have spent hours trying to work this out!

Code:
ErrorHandler:
    Select Case Err
        Case 1004, 13
            sData = "No Data"
            Resume Next
    End Select
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Changing the errror handling back to ...Goto 0 resets (clears) any current existing error. By example:
Code:
Sub exa()
Dim lNumber As Long
    
    On Error Resume Next
    lNumber = "Text"
    
    If Err.Number > 0 Then
        MsgBox Err.Description
    End If
    
    lNumber = "Still Text"
    On Error GoTo 0
    If Err > 0 Then
        MsgBox "I won't show"
    End If
End Sub
 
Upvote 0
Hi,

The problem wasn't so much the error handling, but the fact that the code went through the errorhandler at the end of the sub despite there being no error. It would see the resume statement and try to resume when there was no error to resume from.

Your idea of the if err.number>0 has helped with a fix though! I am now using this, so it doesnt matter that it cycles through at the end. Thanks!!!


Code:
ErrorHandler:
    If Err.Number > 0 Then
        Select Case Err
            Case 1004, 13
                sData = "No Data"
                Resume Next
            Case Else
                On Error GoTo 0
                Resume
        End Select
    End If
 
Upvote 0
Hi,

The problem wasn't so much the error handling, but the fact that the code went through the errorhandler at the end of the sub despite there being no error...

Okay, I misunderstood a bit. Do you have an 'Exit Sub' (or Exit Function) right above ErrorHandler: ? If not, put one in, as this way it only gets to the ErrorHandler: line if there is an error. I am presuming that you have On Error GoTo ErrorHandler someplace up in the code?
 
Upvote 0
Great - that works fantastic - For some reason, I have never liked the exit sub statement, but right above the error handler I guess it does its job :)
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,903
Members
452,948
Latest member
Dupuhini

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