LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Help with find and replace routine

I don't think anyone cares, but here goes.
After about 5 hours of bouncing back and forth between my book, Excel help,
and various message on this forum,
I finally arrived at the following.

Sub changeColumnNames()
' Find a value in a cell and replace it.
' Note that this does no cell selections, it does not
' change the currently selected cell.
'
find_target = "actualAxis1"
new_value = "AZ"
'
Set rng = ActiveSheet.Range("1:1").Find(find_target)

If Not (rng Is Nothing) Then
rng.Formula = new_value
End If

End Sub

There are really only two working lines in he
Set rng = ActiveSheet.Range("1:1").Find(find_target)
rng.Formula = new_value

The remainder just generalizes the function using variables, and protects
from the error of string not found.

Thanks for your time,
Bryan


"Bryan Kelly" wrote in message
.. .
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.





 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and Replace - Replace with Blank Space Studebaker Excel Discussion (Misc queries) 4 April 3rd 23 10:55 AM
Routine to find exact Row matches in Col1 Col2 Col3 but exact offsetting numbers in Col4 [email protected] Excel Discussion (Misc queries) 0 June 11th 08 11:30 PM
where to put results of find operation in find and replace functio DEP Excel Worksheet Functions 5 November 15th 06 07:52 PM
find and replace - replace data in rows to separated by commas msdker Excel Worksheet Functions 1 April 15th 06 01:00 AM
Help needed with FIND routine. Matthew[_8_] Excel Programming 4 October 5th 03 11:50 PM


All times are GMT +1. The time now is 12:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"