ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   If this is not found look for something else or error handling (https://www.excelbanter.com/excel-programming/389292-if-not-found-look-something-else-error-handling.html)

keri

If this is not found look for something else or error handling
 
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.


JE McGimpsey

If this is not found look for something else or error handling
 
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.

In article .com,
keri wrote:

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.


keri

If this is not found look for something else or error handling
 
This was excellent, thanks so much for your time & input.



All times are GMT +1. The time now is 04:03 PM.

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