Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i prevent code interruption

Status
Not open for further replies.

Luis939

MIS
Feb 28, 2003
453
US
I have a big dilemma guys, whenever I want to use the find method to look for a specific string that isnt there, I get an error when the code runs and the program doenst continue executing as intended. So if I have
Cells.Find(What:="Text", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate


but the spreadsheet doesnt have that text in the file so I get an error. Is there any way so that i can say if the Find method doesnt find the word, then to simply continue executing the rest of the code.
 
Don't try to find and activate in the same statement - if no cell is found, it can't be activated, hence the error. Use instead

dim cell as range
set cell=cells.find(....)
if not cell is nothing then
cell.activate
end if

Actually, usually there is no need to activate the cell - you can just use the range variable to do your job, e.g.

if not cell is nothing then
cell.interior.color=vbRed
end if
Rob
[flowerface]
 
Your best bet is to write something like this:
Code:
Dim rangeFound as Range

Set rangeFound = Cells.Find(What:="Text", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Activate
If Not rangeFound Is Nothing then
  'do what you wanna do
Else
  'do something different
End If
Clive [infinity]
 
Sorry...the "Set" line was meant to read as follows:
Set rangeFound = Cells.Find(What:="Text", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False)


Clive [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top