IfError不捕捉types不匹配错误vba

我得到了下面的代码行types不匹配错误。 它在一个循环内,直到第一次迭代才会发生错误.Cells(rowStart + i, ISO_revs_col).Value是一个string。 这是有道理的,这将导致一个错误,但我希望.IfError函数只返回“0”string。 如果有人可以告诉我为什么我得到一个错误,而不是“0”,我将不胜感激。

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

提前致谢。

IFERROR是一个工作表函数,将检测工作表错误。 评估以下错误types:#N / A,#VALUE !, #REF !,#DIV / 0 !, #NUM!,#NAME?或#NULL !.

types不匹配(运行时错误13)是VBA错误,而不是工作表错误。

要处理VBA错误,您需要使用VBAerror handling例程。 所以像这样:

编辑:触发其他错误:

 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 

上面的内容不会告诉你错误发生在哪里,所以你可能只想把单行换行为:

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