ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Trapping a NO FIND after a find (https://www.excelbanter.com/excel-discussion-misc-queries/43191-trapping-no-find-after-find.html)

Alan

Trapping a NO FIND after a find
 
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



FSt1

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


"Alan" wrote:

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



Alan

Hello,

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

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


"Alan" wrote:

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




All times are GMT +1. The time now is 05:22 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com