View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Bryan Kelly Bryan Kelly is offline
external usenet poster
 
Posts: 25
Default Help with find and replace routine

I need to be able to search for some text and replace it. After recording a
macro and editing it I have this

1 Found_name = _
2 Cells.Find(What:="actualAxis2", After:=ActiveCell, LookIn:=xlFormulas, _
3 LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
4 MatchCase:=False).Activate

5 If Found_name = True Then
6 ActiveCell.Replace What:="actualAxis2", Replacement:="EL",
LookAt:=xlPart _
7 , SearchOrder:=xlByRows, MatchCase:=False
8 End If

Lines 2,3,4,6,7 were extracted from the macro I recorded. If the string is
not found, the macro blows up. I added lines 1,5, and 8 to handle the
condition where the find fails. This is not exactly what I had while
working this at work but it should be sufficient to form the question.

How do I write the If statement to bypass the replace statement when the
find is not sucessful?
If you happen to eliminate any items from these statements that are not
needed, that would be cool.

My followup question is:
How can I write a subroutine to the effect:

call replacement( old_text, new_text )

Such that I don't need to copy that code over and over to replace a series
of strings.
I could then write

replacement( "actualAzis1, AZ)
replacement( "actualAzis2, EL)
replacement( "actualAzis3, X)
replacement( "actualAzis4, Y)

and all four replacements would be made. If one string was not found, the
other three replacements would have been completed without error.
--
Thank you,
Bryan Kelly
Time is the medium we use to express out priorities.