If this is not found look for something else or error handling

K

keri

Hi I have this code;

CELLS.find(what:="taf", After:=ActiveCell, LookIn:=xlFormulas,
LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False) _
.Activate

And want to insert either something that says if "taf" is not found
execute the same search but look for sheets("taf").range("k1").value
instead - and then continue doing the rest of the code

However I have tried this without success - so should I just insert
error handling instead (my code is breaking on the cells.find
statement at the moment when it cannot find "taf"). I have never
managed to get my own error handling to work (it seems to execute
whether there is an error or not) so any help on either of these
options would be gratefully recieved.
 
J

JE McGimpsey

One way:

Dim rFound As Range
Set rFound = Cells.Find( _
What:="taf", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rFound Is Nothing Then
Set rFound = Cells.Find( _
What:=Sheets("taf").Range("K1").Value, _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End If
If rFound Is Nothing Then
MsgBox "Neither 'taf' nor taf!K1 value found"
Else
rFound.Activate
'<rest of your code here>
End If

Though I really discourage activating/selecting cells when you can use
the range objects directly.
 

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