If .find method finds nothing?

GBagley

Board Regular
Joined
Feb 8, 2011
Messages
84
I have a macro that uses the find method to search a column for a value. It's within an If statement, but if the value isn't found in the column then I get an 'Object variable not set' error. I would like for it to see the If statement as false in this case. What's the best way to do this without using an On Error statement?

If Sheets(1).Range("A:A").Find(cell).offset(0, 21) = "Y" Then
Variable1 = True
Else
Variable1 = False
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Perhaps like this

Code:
Dim Found As Range
Set Found = Sheets(1).Range("A:A").Find(what:=cell)
If Found Then
    If Found.Offset(0, 21) = "Y" Then
        Variable1 = True
    Else
        Variable1 = False
    End If
Else
    MsgBox "Not found"
    Exit Sub
End If
 
Upvote 0
In fact that could be simplified

Code:
Dim Found As Range
Set Found = Sheets(1).Range("A:A").Find(what:=cell)
If Found Then
    variable1 = Found.Offset(0, 21) = "Y"
Else
    MsgBox "Not found"
    Exit Sub
End If
 
Upvote 0
Perhaps like this

Code:
Dim Found As Range
Set Found = Sheets(1).Range("A:A").Find(what:=cell)
If Found Then
    If Found.Offset(0, 21) = "Y" Then
        Variable1 = True
    Else
        Variable1 = False
    End If
Else
    MsgBox "Not found"
    Exit Sub
End If

Hmm...closer, but I'm getting a type mismatch on "If Found Then". Found is returning a string (the contents of the cell). If I do Find(what:=cell).Address, then I get an object variable not set error.
 
Upvote 0
Code:
Dim Found As Range
Set Found = Sheets(1).Range("A:A").Find(what:=cell)
If Found Is Nothing Then
    Else
    variable1 = Found.Offset(0, 21) = "Y"
End If

Ah, I think I figured it out. 'If Found Is Nothing Then' worked.


While we're here, is there a syntax for the reverse, 'If Found Is Not Nothing'? I tried 'If Found Not Nothing' but the VBE autocorrects it to 'If Not Found', is that the same or is it doing something different than I imagine?
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,119
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top