从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. 产生错误
  2. 测试错误

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

编辑#1:

这是一种测试任何错误和特定单元格中的错误的方法:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章