check a “#N/A” value in vba into a range

2019-02-22 10:58发布

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

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.

Anyone can help me plz ?

2条回答
姐就是有狂的资本
2楼-- · 2019-02-22 11:51

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:

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
查看更多
3楼-- · 2019-02-22 11:54

Another way to check for the error

If CVErr(MyTab(i, j).Value) = CVErr(xlErrNA)
查看更多
登录 后发表回答