IfError 未捕获类型类型不匹配错误 vba
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26726071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IfError isn't catching type type mismatch error vba
提问by nateAtwork
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.
我在下面的代码行中遇到类型不匹配错误。它在一个循环内,直到第一次迭代 where.Cells(rowStart + i, ISO_revs_col).Value
是一个字符串时才会发生错误。这会导致错误是有道理的,但我希望该.IfError
函数只返回“0”字符串。如果有人能告诉我为什么我收到错误而不是“0”,我将不胜感激。
Debug.Print Application.WorksheetFunction.IfError(CLng(.Cells(rowStart + i, _
ISO_revs_col).Value), "0")
Thanks in advance.
提前致谢。
回答by Ron Rosenfeld
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!.
IFERROR 是一个工作表函数,将检测工作表错误。评估以下错误类型:#N/A、#VALUE!、#REF!、#DIV/0!、#NUM!、#NAME? 或 #NULL!。
Type mismatch (runtime error 13) is a VBA error, not a worksheet error.
类型不匹配(运行时错误 13)是 VBA 错误,而不是工作表错误。
To handle VBA errors, you need to use the VBA error handling routines. So something like:
要处理 VBA 错误,您需要使用 VBA 错误处理例程。所以像:
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