Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Getting info from FIND/REPLACE

I run something like:

Sub Macro2()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Activate
Cells.Replace What:="xxx", Replacement:="yyy"
Next
End Sub

This runs over many, many sheets. Most sheets don't have the string. Is
there any way I can return something from Replace to tell me if any
replacements were actually made in a given sheet?

I would like to break out of the loop once I get to the sheet that contained
the string in question.
--
Gary's Student
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 196
Default Getting info from FIND/REPLACE

Hi Gary

Not using Replace, but does the following help?

Sub Macro2()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Activate
Set rng = Cells.Find(what:="xxx")
If Not rng Is Nothing Then
Cells.Replace what:="xxx", Replacement:="yyy"
MsgBox "found&replaced"
Exit Sub
End If
Next
End Sub

Richard

PS: Season's Greetings to you & your family!


Gary''s Student wrote:
I run something like:

Sub Macro2()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Activate
Cells.Replace What:="xxx", Replacement:="yyy"
Next
End Sub

This runs over many, many sheets. Most sheets don't have the string. Is
there any way I can return something from Replace to tell me if any
replacements were actually made in a given sheet?

I would like to break out of the loop once I get to the sheet that contained
the string in question.
--
Gary's Student


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Getting info from FIND/REPLACE

This is excellent and fully meets my needs.


You have given me something far better than a lump of coal. Merry Christmas.
--
Gary's Student


"RichardSchollar" wrote:

Hi Gary

Not using Replace, but does the following help?

Sub Macro2()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Activate
Set rng = Cells.Find(what:="xxx")
If Not rng Is Nothing Then
Cells.Replace what:="xxx", Replacement:="yyy"
MsgBox "found&replaced"
Exit Sub
End If
Next
End Sub

Richard

PS: Season's Greetings to you & your family!


Gary''s Student wrote:
I run something like:

Sub Macro2()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Activate
Cells.Replace What:="xxx", Replacement:="yyy"
Next
End Sub

This runs over many, many sheets. Most sheets don't have the string. Is
there any way I can return something from Replace to tell me if any
replacements were actually made in a given sheet?

I would like to break out of the loop once I get to the sheet that contained
the string in question.
--
Gary's Student



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default Getting info from FIND/REPLACE

Just wrap the 'for each ws' around this code..

Sub ReplaceIt()
Dim Ct As Long, ws As Worksheet
Set ws = ActiveSheet 'required for testing only
'formula for the whole cell
'Ct = [countif(C2:C14,"*xxx*")]
'formula for xlPart - so this is the one we use ;)
Ct = [Sumproduct(Len(C2:C14)-Len(Substitute(C2:C14,"xxx","")))]
If Ct < 0 Then
[C2:C14].Replace What:="xxx", _
Replacement:="yyy", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
MsgBox Ct & " matches found"
Else
MsgBox "No matches found for " & ws.Name
End If
End Sub


... and don't forget to replace the hard coded range above.


Regards
Robert McCurdy
"Gary''s Student" wrote in message ...
I run something like:

Sub Macro2()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Activate
Cells.Replace What:="xxx", Replacement:="yyy"
Next
End Sub

This runs over many, many sheets. Most sheets don't have the string. Is
there any way I can return something from Replace to tell me if any
replacements were actually made in a given sheet?

I would like to break out of the loop once I get to the sheet that contained
the string in question.
--
Gary's Student
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Getting info from FIND/REPLACE

Thank you Robert and have a merry Christmas.
--
Gary's Student


"Robert McCurdy" wrote:

Just wrap the 'for each ws' around this code..

Sub ReplaceIt()
Dim Ct As Long, ws As Worksheet
Set ws = ActiveSheet 'required for testing only
'formula for the whole cell
'Ct = [countif(C2:C14,"*xxx*")]
'formula for xlPart - so this is the one we use ;)
Ct = [Sumproduct(Len(C2:C14)-Len(Substitute(C2:C14,"xxx","")))]
If Ct < 0 Then
[C2:C14].Replace What:="xxx", _
Replacement:="yyy", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
MsgBox Ct & " matches found"
Else
MsgBox "No matches found for " & ws.Name
End If
End Sub


... and don't forget to replace the hard coded range above.


Regards
Robert McCurdy
"Gary''s Student" wrote in message ...
I run something like:

Sub Macro2()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Activate
Cells.Replace What:="xxx", Replacement:="yyy"
Next
End Sub

This runs over many, many sheets. Most sheets don't have the string. Is
there any way I can return something from Replace to tell me if any
replacements were actually made in a given sheet?

I would like to break out of the loop once I get to the sheet that contained
the string in question.
--
Gary's Student

Reply
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
find and replace - replace data in rows to separated by commas msdker Excel Worksheet Functions 1 April 15th 06 01:00 AM
Using Find and Replace to replace " in a macro snail30152 Excel Programming 1 April 13th 06 11:58 PM
Replace method - cannot find any data to replace Mike Excel Programming 5 April 6th 06 08:56 PM
Code to search and replace info on modules Brett Smith[_2_] Excel Programming 2 January 12th 06 08:58 PM


All times are GMT +1. The time now is 10:24 AM.

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

About Us

"It's about Microsoft Excel"