0

I have the following code which given a cellrow and cellcolumn returns the value in a given Excel worksheet:

Public Function getCellValue(cellRow, cellColumn) As String
    Dim aString As String
    Dim xRng As Excel.Range
    Dim val As Object

    xRng = CType(xlSheet.Cells(cellRow, cellColumn), Excel.Range)
    val = xRng.Value()
    If val Is DBNull.Value Then 'thought this would work
        aString = "Null"
    Else
        aString = val.ToString
    End If
    Return aString
End Function

My issue is sometimes the cell is going to be empty. I thought the DBNull.Value check would work but it gives me an error. Any and all help is much appreciated!

ERROR: An unhandled exception of type 'System.NullReferenceException' occurred in Tutorial.exe Additional information: Object reference not set to an instance of an object.`

EDIT: I know what a nullreferenceexception is, I am just looking for a way to check if an excel value IS null

Bob
  • 1,344
  • 3
  • 29
  • 63

0 Answers0