Error Trapping

G

Guest

How do I generate an error message such as "Macro will be aborted - The Find
Function cannot return a valid value" if the string "Cross" is not found in
the selection.

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

Thanks
 
B

Bob Phillips

Set cell = Selection.Find(What:="Cross", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If cell Is Nothing Then
MsgBox "Macro will be aborted - The Find Function cannot return a valid
value"
Exit sub
end if

--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)
 
G

Guest

use a range object and test if it is nothing something like this...

dim rng as range

set rng = Selection.Find(What:="Cross", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

if rng is nothing then
msgbox "Macro will be aborted - The Find Function cannot return a valid
value"
else
rng.activate
end if
 
G

Guest

Hi Rafi,

you could use this:

Public Sub Find_Cross()
Dim rngFound As Range

Set rngFound = Selection.Find(What:="Cross", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If rngFound Is Nothing Then
MsgBox "Macro will be aborted - The Find Function cannot return a
valid value"
Else
rngFound.Activate
End If

End Sub

Did this help you?

Best wishes,
Eric
 

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