1

I have an Excel spreadsheet. I'm trying to test whether the formula results in an error. VBA has VBA.IsError and I'm looking for an equivalent in VB.net.

    Dim CurrCell As Excel.Range = ws.Range("B10")
    CurrCell.Formula = "=DATEVALUE(-1)" 'results in #VALUE! error
    CurrCell.Select()

    'doesn't detect error
    If Microsoft.VisualBasic.IsError(CurrCell.Value) Then
        Debug.Print("error")
    Else
        Debug.Print("not error")
    End If

    'doesn't detect error; shows as VariantType.Integer
    Debug.Print(VarType(CurrCell.Value))
    If VarType(CurrCell.Value) = VariantType.Error Then
        Debug.Print("error")
    Else
        Debug.Print("not error")
    End If

How do I detect whether the result of the formula is an error using VB.net?

D_Bester
  • 5,723
  • 5
  • 35
  • 77

1 Answers1

2

This works:

    If xlApp.WorksheetFunction.IsError(CurrCell) Then
        Debug.Print("error")
    Else
        Debug.Print("not error")
    End If

But a much better solution was given by Mike Rosenblum

    Public Shared Function IsError(ByVal rgValue As Object) As Boolean
        Return TypeOf (rgValue) Is Int32
    End Function

Dealing with CVErr Values in .NET – Part I: The Problem

Dealing with CVErr Values in .NET – Part II: Solutions

Community
  • 1
  • 1
D_Bester
  • 5,723
  • 5
  • 35
  • 77