IsNA无法识别已closures的工作簿参考中的#N / A

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

Sub TestTest() 'Declaring variables [BD] Dim sFilePath As String Dim sFileName As String Dim sSourceSheet As String Dim sSourceCell As String sFileName = "0306-0312 Margin Master.xlsx" sFilePath = "\\store\GroupDrives\Pricing\_Deli_\Deli Fresh Shift\Margin Master\" sSourceSheet = "Bakery" sSourceCell = "G5" If Application.WorksheetFunction.IsNA("'" & sFilePath & "[" & sFileName & "]" & sSourceSheet & "'!" & _ Range("A1").Range(sSourceCell).Address(, , xlR1C1)) Then MsgBox "Hay!" Else MsgBox "Good to go!" End If End Sub 

尝试使用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值。

另一种方法是使用Evaluate()和“正则expression式”,即

 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 #N/A的string,则可以调整If语句来检查评估单元格值。

注:细胞参考需要是R1C1风格,我相信。