On Error Goto Errhandler: not working

CodyMonster

Board Regular
Joined
Sep 28, 2009
Messages
159
I'm kind of surpised this doesn't work.
I'm getting the 91 error when WellIDRow doesn't find what it's looking for, which I expected, that is why I put in the error handing. If it can't find the number then it should skip and go to the next "a".
However, I'm still getting the "pop-up" of the Run-time error '91':
I thought it would go right to ErrHandler: when the error happens.. but it isn't.
anyone know what I'm doing wrong here? :confused:


Thanks

Code:
With id.Range("C:C")
    Set SAPWell = .Find("SAP Well Code", LookIn:=xlValues)
    SAPwellRow = SAPWell.Row
    LastRowSAP = id.Cells(Rows.Count, "C").End(xlUp).Row
End With


For a = SAPwellRow + 1 To LastRowSAP


On Error GoTo ErrHandler:


    SAPWC = id.Cells(a, 3).Value
    If IsEmpty(SAPWC) = False Then
        WellIDRow = wi.Range("C:C").Find(SAPWC, LookIn:=xlValues).Row
        wi.Cells(WellIDRow, 13).Value = Format(id.Cells(a, 8).Value, "#0.0000")
        wi.Cells(WellIDRow, 13).NumberFormat = "#0.0000"
        wi.Cells(WellIDRow, 1).Value = id.Cells(a, 1).Value
        Else
    End If


ErrHandler:
    If Err.Number = 91 Then
    Err.Clear
    End If


Next


On Error GoTo 0
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Two possibilities.

First, when you use an error handler, you are required to end it with Resume, Resume Next, or Resume Location. Otherwise, the error handler assumes you are still in the error handler. So you're probably getting a 91, jumping to ErrHandler:, going back to the top of the loop, then you're getting ANOTHER 91, and then you get the error. You could rewrite your code something like:

Code:
Sub test2()

    With ID.Range("C:C")
        Set SAPWell = .Find("SAP Well Code", LookIn:=xlValues)
        SAPwellRow = SAPWell.Row
        LastRowSAP = ID.Cells(Rows.Count, "C").End(xlUp).Row
    End With

    On Error GoTo ErrHandler:
    
    For a = SAPwellRow + 1 To LastRowSAP

        SAPWC = ID.Cells(a, 3).Value
        If IsEmpty(SAPWC) = False Then
            WellIDRow = wi.Range("C:C").Find(SAPWC, LookIn:=xlValues).Row
            wi.Cells(WellIDRow, 13).Value = Format(ID.Cells(a, 8).Value, "#0.0000")
            wi.Cells(WellIDRow, 13).NumberFormat = "#0.0000"
            wi.Cells(WellIDRow, 1).Value = ID.Cells(a, 1).Value
        Else
        End If
ResumeHere:
    Next a
    
    Exit Sub


ErrHandler:
    If Err.Number = 91 Then Err.Clear
    Resume ResumeHere:

End Sub

The other option is to change how you do the Find. Try:

Code:
    If IsEmpty(SAPWC) = False Then
        Set FindCell = wi.Range("C:C").Find(SAPWC, LookIn:=xlValues)
        If Not FindCell Is Nothing Then
             WellIDRow = FindCell.Row
             wi.Cells(WellIDRow, 13).Value = Format(ID.Cells(a, 8).Value, "#0.0000")
             wi.Cells(WellIDRow, 13).NumberFormat = "#0.0000"
             wi.Cells(WellIDRow, 1).Value = ID.Cells(a, 1).Value
         End If
     Else
     End If
This should prevent the error altogether.
 
Last edited:
Upvote 0
Glad it works for you. And when you end up needing an error handler in the future, you know a bit more about it.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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