using search in a macro

M

Matt Houston

Hello, hoping for some help.

I have macro to search for a specific bit of data in my spreadsheet
but when excel can't find it (because it may not be there) I would lik
the macro to stop instead of collapsing and giving me this Error 91 an
then "cannot find data blah blah blah" messagebox. Any ideas
 
D

Dave Peterson

the most common suggestion is to check to see what happened:

Sub Testme01()
Dim FoundCell As Range

with activesheet
Set FoundCell = .Cells.Find(what:="what???",...rest of find command)
If FoundCell Is Nothing Then
'it wasn't found
Else
'it was found
End If
end with
End Sub
 
M

Matt Houston

thanks for the help. got the error handler to work, now I need to exi
the macro. If i put exit sub it just exits the function I am in, how d
I exit the whole macro...if that makes sense
 
D

Dave Peterson

Maybe you could have your function return a value that can be checked by the
calling routine:

option explicit
sub test()
dim OkToContinue as boolean
oktocontinue = myFindFunction(whateveryou'repassinghere)
if oktocontinue = false then
exit sub
end if
'otherwise, keep going.
end sub

function myfindfunction(whateveryouexpecthere) as boolean
Dim FoundCell As Range
with activesheet
Set FoundCell = .Cells.Find(what:="what???",...rest of find command)
If FoundCell Is Nothing Then
myfindfunction = false
Else
myfindfunction = true
End If
end with
End Sub
 
Top