将 vba 中的“#N/A”值检查到一个范围内

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11394029/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:28:28  来源:igfitidea点击:

check a "#N/A" value in vba into a range

excelvbaerror-handlingexcel-vba

提问by Adrien A.

I want to check a #N/Avalue into excel with VBA. So after some research, I made this code :

我想#N/A用 VBA将一个值检查到 excel 中。所以经过一些研究,我做了这个代码:

Set MyTab = Range(name)
If (Not IsEmpty(MyTab.value)) And (MyTab(i, j).value <> CVErr(xlErrNA)) Then
   Call BuildRequest(False, id, MyTab, i, j)
End If

But when it passed on MyTab(i, j).value <> CVErr(xlErrNA)I have an error 13(type error)and I don't find why.

但是当它传递时,MyTab(i, j).value <> CVErr(xlErrNA)我有一个错误13(type error),我不知道为什么。

Anyone can help me plz ?

任何人都可以帮助我吗?

回答by assylias

You first need to check that the cell does contain an error:

您首先需要检查单元格是否包含错误:

If IsError(MyTab(i, j).Value) Then
    If MyTab(i, j).Value <> CVErr(xlErrNA) Then

Unless you do want to know the type of error (#N/A, #DIV/0!, etc) , you might as well replace your test with:

除非您确实想知道错误的类型 (#N/A, #DIV/0!, etc),否则您最好将测试替换为:

If (Not IsEmpty(MyTab.value)) And (Not IsError(MyTab(i, j).value)) Then

If you need to check the error type, you can write:

如果需要检查错误类型,可以这样写:

Dim shouldBuildRequest As Boolean

shouldBuildRequest = Not IsEmpty(MyTab.value)
If IsError(MyTab(i, j).Value) Then
    shouldBuildRequest = shouldBuildRequest AND (MyTab(i, j).Value <> CVErr(xlErrNA))
End If

If shouldBuildRequest Then
    Call BuildRequest(False, id, MyTab, i, j)
End If

回答by Siddharth Rout

Another way to check for the error

检查错误的另一种方法

If CVErr(MyTab(i, j).Value) = CVErr(xlErrNA)