IsNA在关闭的工作簿参考中无法识别#N / A.

时间:2017-05-23 20:03:49

标签: excel vba excel-vba

我正在尝试测试封闭的外部工作簿中的单元格是否为N / A.在下面的代码中,引用的工作簿中的单元格“G5”肯定是N / A,但是当使用下面的IsNA函数引用它时,它返回“Good to go!”。当它的目的是让它返回“干草!”在消息框中。

/

2 个答案:

答案 0 :(得分:3)

尝试使用ExecuteExcel4Macro

Sub TestTest()

    'Declaring variables [BD]
    Dim sFilePath As String
    Dim sFileName As String
    Dim sSourceSheet As String
    Dim sSourceCell As String
    dim externalValue As Variant

    sFileName = "0306-0312 Margin Master.xlsx"
    sFilePath = "\\store\GroupDrives\Pricing\_Deli_\Deli Fresh Shift\Margin Master\"
    sSourceSheet = "Bakery"
    sSourceCell = "G5"

    externalValue = ExecuteExcel4Macro("'" & sFilePath & "[" & sFileName & "]" & sSourceSheet & "'!" & _
        Range("A1").Range(sSourceCell).Address(, , xlR1C1)) 
    If Application.IsNa(externalValue) Then
        MsgBox "Hay!"
    ElseIf IsError(externalValue) Then
        MsgBox "May not work"
    Else
        MsgBox "Good to go! (value is '" & externalValue & "')"
    End If

End Sub

注意:如果您只使用“G5”等单元格引用作为Range("A1").Range(sSourceCell).Address(, , xlR1C1)的值,则Range(sSourceCell).Address(, , xlR1C1)可能缩写为sSourceCell

答案 1 :(得分:2)

另一种选择可能是使用Evaluate()和#34;常规公式",即

Sub TestTest()

'Declaring variables [BD]
Dim sFilePath As String, sFileName As String, sSourceSheet As String, sSourceCell As String

sFileName = "0306-0312 Margin Master.xlsx"
sFilePath = "\\store\GroupDrives\Pricing\_Deli_\Deli Fresh Shift\Margin Master\"
sSourceSheet = "Bakery"
sSourceCell = "R5C7"

If Application.WorksheetFunction.IsError(Evaluate("=('" & sFilePath & "[" & sFileName & "]" & sSourceSheet & "'!" & sSourceCell & ")")) Then
    MsgBox "Hay!"
Else
    MsgBox "Good to go!"
End If

End Sub

如果单元格确实是#N/A错误,它应该适用于您。如果它只是一个#N/A的字符串,您只需调整If语句即可检查评估单元格值。

注意:我相信Cell Reference必须是R1C1样式。