How do I make an integer to null in Excel VBA?

Arlen Beiler picture Arlen Beiler · Oct 26, 2010 · Viewed 30.6k times · Source

I am trying to detect whether an integer was set, and if not, skip most of the code in the loop (using an if statement). Here is what I have so for.

Do While hws.Cells(r, 9).Value <> ""
    On Error Resume Next
    ar = Null
    ar = aws.Range("A:A").Find(hws.Cells(r, 2).Value).Row
    If Not IsNull(ar) Then
  'work with ar'
    End If
    r = r + 1
Loop

However, when I run it, ar = Null has problems. It says "Invalid use of null".

Answer

Foole picture Foole · Oct 26, 2010

Variables defined as Integer cannot be Null in VBA. You will have to find another way to do what you want. eg use a different data type or use a magic number to indicate null (eg -1).

In your example code, either ar will be assigned a Long value (Range.Row is a Long) or it will throw an error.