Trapping a NO FIND after a find

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I use the code below to store a row number to a variable after a find.

I would like to trap a NO FIND if the find is unsuccessfull
Any ideas. FSt1 provided the code below

Sub macfindrow()
dim rn as string
dim rng as range
dim therow as long

rn = inputbox("enter something to find")
if rn <> "" then
Set rng = nothing
Set rng = range("A1:IV65536").Find(what:=rn, _
After:=Range("A1"), _
Lookin:=xlformulas, _
Lookat:=xlpart, _
SearchOrder:=xlbyrows, _
SearchDirection:=xlNext, _
MatchCase:=false)
end if
therow = rng.row
msgbox "Found at cell " & rng.address
msbbox "The row number is " & therow
end sub
 
hi,
if nothing was found, how do you trap nothing?
add this to the code before the other msgboxes.

If rng Is Nothing Then
MsgBox rn & " was Not found."
exit sub
End If

regards
FSt1
 
Hello,

Thanks again.
I can now go off and do something else within the script if nothing is found
 

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

Back
Top