从Excel电子表格中读取错误到VBA中

我试图确定一个特定的单元格是否包含错误值。 我一直在尝试使用IsError函数,但不知道使用什么语法。

如果单元格D4包含一个#N / A这里是我目前的代码:

Dim curCol As String Dim tmp As Boolean curCol = "$D$" tmp = WorksheetFunction.IsError(TListSheet.Cells(curCol & "4").Values) 

上面的最后一行结束函数的执行,在调用它的单元格中产生一个#VALUE。 TListSheet是我正在使用的工作表的代码名称和另一个返回curCol为$ D $的函数,所以我不能只是将该值硬编码到IsError函数

这是一个简单的方法来:

  1. 创build一个错误
  2. testing错误

 Sub MakeErrorTestError() Range("B9").Formula = "=VLOOKUP(0,0,0)" MsgBox Range("B9").Text End Sub 

编辑#1:

以下是testing特定单元格中的任何错误和错误的方法:

 Sub MakeErrorTestError() Dim rError As Range Range("B9").Formula = "=VLOOKUP(0,0,0)" On Error Resume Next Set rError = ActiveSheet.UsedRange.Cells.SpecialCells(xlCellTypeFormulas, xlErrors) If rError Is Nothing Then MsgBox "No errors" If Intersect(Range("B9"), rError) Is Nothing Then MsgBox "No error in B9" Else MsgBox "error in B9" End If End Sub