IfError没有捕获类型类型不匹配错误vba

时间:2014-11-04 01:00:05

标签: excel vba error-handling

我在下面的代码行中遇到类型不匹配错误。它位于循环内,并且在.Cells(rowStart + i, ISO_revs_col).Value为字符串的第一次迭代之前不会发生错误。这有意义,这会导致错误,但我希望.IfError函数只返回“0”字符串。如果有人能告诉我为什么我得到错误而不是“0”我会很感激。

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

提前致谢。

1 个答案:

答案 0 :(得分:3)

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

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

要处理VBA错误,您需要使用VBA错误处理例程。如下所示:

编辑以触发其他错误:

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
相关问题