views:

62

answers:

3

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".

+1  A: 

Hi, just use a variant and isempty:

Dim i

If IsEmpty(i) Then MsgBox "IsEmpty"
i = 10

If IsEmpty(i) Then
   MsgBox "IsEmpty"
Else
   MsgBox "not Empty"
End If
i = Empty
If IsEmpty(i) Then MsgBox "IsEmpty"
'a kind of Nullable behaviour you only can get with a variant
'do you have integer?
Dim j as Integer
j = 0
If j = 0 Then MsgBox "j is 0"
Oops
No thanks, I like defining everything. Can I set it to empty again.
Arlen Beiler
so what type is the variable "ar" in your code then?
Oops
It is an integer.
Arlen Beiler
as to the variant: no matter what you write: "Dim i as Variant" is the same as "Dim i" both times you have defined a Variants
Oops
I know.........
Arlen Beiler
+3  A: 

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.

Foole
+1  A: 

Find returns a range:

Dim rf As Range
With aws.Range("A:A")
    Set rf = .Find(hws.Cells(r, 2).Value)
    If Not rf Is Nothing Then
        Debug.Print "Found : " & rf.Address
    End If
End With

-- http://msdn.microsoft.com/en-us/library/aa195730(office.11).aspx

Remou