2

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

1
  • @Siddharth sorry, I miss read it, it was an edit by the OP, removing my comment Commented Mar 6, 2012 at 10:43

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

10 Comments

actually i dont want to print any error message instead straightaway skip to the next iteration
but vba says if it encounters an error in a function it will check for error handling in the calling sub.so shoudnt it pass to main???and moreover putting it in the function wont allow me to skip a certain percentage of my code which will produce an error in main
your code isnt working as i said,it doesnt skip "the lines of code it should have" and thus produces an overflow error(which vba doesnt handle)
@Kartik Anand: I have updated the code now. Please check if you still get the errors.
sorry but i cant upload anything from my office computer!!i'll surely reply with the snapshots when i get back home;
|
0

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

14 Comments

Actually i dont want to use resume next because there is a lot of code between errorhandler and find function and will produce overflow if allowed to run.That is why i straight away want to skip to the next iteration.
Thank you very much your recommendations,worked like a charm;and just for curiosity why didnt it work with "on error goto errorhandler" ??
Because you did not have a Err.Clear to reset the error handling
@Chis neilsen: if you put OERN right at the top then it will suppress all errors. For example if you don't have a Sheet("Input") then OERN will simply suppress the error instead of highlighting it. If I go by your method then I will use OERN inside the loop just before marks = Find(name) :)
@chris neilsen: I disagree with the point that "imbedding a error handler inside a loop is a poor design" i.e if you are referring to OERN. There are many scenarios where you have to use OERN inside a loop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.