Kartik Anand
Kartik Anand

Reputation: 4609

VBA error handling not working when function being called generates error

I am iterating through rows,and looking up the first column of each row(name) using a different function for finding his marks.

For each "name" there is a particular entry in a different table("marks") which can also be blank or "-"

Sub main()
On error goto errorhandler
Dim name as string
Dim marks as double
Dim source as range
Dim runs as integer

runs = 1
Set source = Sheets("input").Range("$A$2")
i=1

Do until source.offset(i,0) = ""        'iterate through rows 

    name = source.offset(i,0)
    marks = find(name)

    do until runs * marks > 100
        runs = runs + 1 'since marks is not defined;runs overflows 
    Loop
    'a lot of code which relies on marks
errorhandler:
    i = i + 1
Loop

End Sub

Function find(name as string) as double
    find = application.vlookup(name,Sheets("values").Range("$A$2,$C$5"),2,0)
End function

now as i said the value in column 2 of that table can also be blank or "-" and thus results in error Runtime error 13 "Type mismatch"

i even tried putting on error statement inside the loop VBA should normally search for error handling in the calling function i.e "main" but its not doing so

Upvotes: 2

Views: 3099

Answers (2)

Siddharth Rout
Siddharth Rout

Reputation: 149305

TRIED AND TESTED

Sub main()
    On Error GoTo errorhandler

    Dim name As String
    Dim marks As Double
    Dim source As Range

    Set source = Sheets("input").Range("$A$2")
    i = 1

    Do Until source.Offset(i, 0) = ""       'iterate through rows
        name = source.Offset(i, 0)
        marks = find(name)
        Debug.Print marks
        i = i + 1
    Loop

    Exit Sub
errorhandler:
    MsgBox Err.Description
End Sub

Function find(name As String) As Double
    find = Application.WorksheetFunction.VLookup(name, Sheets("values").Range("$A$2:$C$5"), 2, False)
End Function

EDIT: Kartik, Sorry, I didn't see that you have already accepted the answer.

FOLLOWUP

actually i dont want to print any error message instead straightaway skip to the next iteration – Kartik Anand 14 secs ago

In that case you are handling error in the wrong section ;)

Try this

Sub main()
    Dim name As String
    Dim marks As Double
    Dim source As Range

    Set source = Sheets("input").Range("$A$2")
    i = 1

    Do Until source.Offset(i, 0) = ""       'iterate through rows
        name = source.Offset(i, 0)
        marks = find(name)
        Debug.Print marks
        i = i + 1
    Loop
End Sub

Function find(name As String) As Double
    On Error GoTo earlyexit

    find = Application.WorksheetFunction.VLookup(name, Sheets("values").Range("$A$2:$C$5"), 2, False)

    Exit Function
earlyexit:
    find = 0
End Function

Upvotes: 2

chris neilsen
chris neilsen

Reputation: 53126

Add an Err.Clear after errorhandler:

Also, see the Excel help on Err.Clear which reccomends On Error Resume Next
together with If Err.Number <> 0 Then

This will produce much clearer code

Something like

Sub main()
    On Error Resume Next
    Dim name As String
    Dim marks As Double
    Dim source As Range

    Set source = Sheets("input").Range("$A$2")
    i = 1

    Do Until source.Offset(i, 0) = ""       'iterate through rows
        name = source.Offset(i, 0)
        marks = Find(name)
        If Err.Number <> 0 Then
            Err.Clear
        Else
            ' Your other code for non-error case here

        End If
        i = i + 1
    Loop
End Sub

Upvotes: 0

Related Questions