If find function to not find anything

C

Carlos

Hi,

I basically want to run an if function that searches a range to find a
variable. (hopefully not finding it) then it will run the code.

if it does find a match I want it to just post a msgbox.


What code syntax do i need for find nothing.

Sub test ()

S = 10

Worksheets("sheet1").Range("A1:A30").Select

Cells.Find(What:=S, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

If find Is Nothing Then

' run my code

Else

' msgbox.


end if
end sub

Thanks for any help

Carl
 
J

Joel

Sub test ()

S = 10

set SearchRange = Worksheets("sheet1").Range("A1:A30")

set c = SearchRange.Find(What:=S, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If c Is Nothing Then

' run my code

Else

' msgbox.


end if
end sub
 
J

Jim Thomlinson

You should probably drop the After:=ActiveCell from the code. If the search
range is not on the active sheet then your code will bomb. Since all you are
doing is checking for the existence of S it does not matter where you start...
 
C

Carlos

Thanks boths,

Works great but did need to the remove the after:=activeCell.

Cheers
Carl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top