Home » excel » excel – IfError isn't catching type type mismatch error vba

excel – IfError isn't catching type type mismatch error vba

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am getting a type mismatch error on the line of code below. It is inside a loop, and the error doesn’t occur until the first iteration where .Cells(rowStart + i, ISO_revs_col).Value is a string. It makes sense that this would cause an error, but I would expect the .IfError function to just return the “0” string. If anyone can tell me why I am getting an error instead of “0” I would appreciate it.

Debug.Print Application.WorksheetFunction.IfError(CLng(.Cells(rowStart + i, _
                                        ISO_revs_col).Value), "0")

Thanks in advance.

How to&Answers:

IFERROR is a worksheet function, and will detect worksheet errors. The following error types are evaluated: #N/A, #VALUE!, #REF!, #DIV/0!, #NUM!, #NAME?, or #NULL!.

Type mismatch (runtime error 13) is a VBA error, not a worksheet error.

To handle VBA errors, you need to use the VBA error handling routines. So something like:

EDIT: to trigger on other errors:

On Error Resume Next
    'your looping routine start
x = CLng(.Cells(rowStart + i, ISO_revs_col).Value)
select case err.number
    case 13
       x=0
       err.clear
    case <> 0
       msgbox "Error " & err.number & vbTab & err.description
 end select
debug.print x
    'your looping routine end
on error goto 0

The above won’t tell you where the error occurred, so you might want to just wrap the single line as:

on error resume next
x = CLng(.Cells(rowStart + i, ISO_revs_col).Value)
if err.number = 13 then x = 0
on error goto 0