Using Selection.Find in a macro

  • Thread starter Erik K via OfficeKB.com
  • Start date
E

Erik K via OfficeKB.com

I have set a selection find in a macro to find the word "Alternate" and
replace it with a name. The problem is that if it does not find the word
"Alternate" the macro doesn't run. Help!
 
T

Tony Jollans

Would you care to try again - and perhaps post some code?

If the macro doesn't run, it neither finds nor not finds anything and,
anyway, what do you want it to do when it doesn't find your string -
obviously it can't replace something it can't find.
 
E

Erik K via OfficeKB.com

How is this?:

Range("V38,V45,V52,V58,V65,V72,V78,V84").Select
If Range("AG21").Value = "N" And ActiveCell <> "Tony" Then
Selection.Find(What:="Alternate", After:=ActiveCell, LookIn:
=xlFormulas,
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.FormulaR1C1 = "Tony"
ElseIf Range("AG24").Value = "N" And ActiveCell <> "Robert" Then
Range("V12,V17,V24,V28,V34,V38,V44,V50,V57,V63,V69,V74,V79,V85").
Select
Selection.Find(What:="Alternate", After:=ActiveCell, LookIn:
=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.FormulaR1C1 = "Robert"
End If
 
E

Erik K via OfficeKB.com

What I want with this is to find any cell that has "Alternate" in it and
replace it with Tony. If there are not any "Alternate" cells, then I want to
continue on with the next part of my macro.
 
T

Tony Jollans

This is an Excel problem and would be better asked in an Excel group.

In brief, you cannot Activate a cell which hasn't been found and you need to
do something like ..

Set TempRange = Selection.Find(etc...=False)
If not TempRange is nothing then Temprange.Activate
 

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