Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Find and Replace in VBA

So I've got this code to perform a find-and-replace in an Excel
workbook:

Cells.Replace What:="This", Replacement:="That", _
LookAt:=xlPart, SearchOrder:=xlByColumns, _
MatchCase:=False, SearchFormat:=False,
ReplaceFormat:=False

Works fine, except that in order to have it perform the find-and-
replace on all worksheets in the workbook, I have to type CTRL-H and
change the "Within" property from "Sheet" to "Workbook". The next time
I open Excel, that property is automatically changed back to "Sheet."

Is there a way to set the "Within" property for find-and-replace in
VBA?

Thanks!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Find and Replace in VBA

Assuming that line of code does what you want, just replace it with this
loop...

For Each WS In Worksheets
WS.Cells.Replace What:="This", Replacement:="That", _
LookAt:=xlPart, SearchOrder:=xlByColumns, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next

and use this line of code to declare the WS variable...

Dim WS As Worksheet

The above loop will loop through each worksheet and run the Replace method
on their cells automatically.

--
Rick (MVP - Excel)


"septimus" wrote in message
...
So I've got this code to perform a find-and-replace in an Excel
workbook:

Cells.Replace What:="This", Replacement:="That", _
LookAt:=xlPart, SearchOrder:=xlByColumns, _
MatchCase:=False, SearchFormat:=False,
ReplaceFormat:=False

Works fine, except that in order to have it perform the find-and-
replace on all worksheets in the workbook, I have to type CTRL-H and
change the "Within" property from "Sheet" to "Workbook". The next time
I open Excel, that property is automatically changed back to "Sheet."

Is there a way to set the "Within" property for find-and-replace in
VBA?

Thanks!


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Find and Replace in VBA

Try the below which works for the entire workbook

Sub Macro()
Dim ws As Worksheet
For Each ws In Sheets
ws.Cells.Replace What:="This", Replacement:="That", _
LookAt:=xlPart, SearchOrder:=xlByColumns, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next
End Sub

--
Jacob


"septimus" wrote:

So I've got this code to perform a find-and-replace in an Excel
workbook:

Cells.Replace What:="This", Replacement:="That", _
LookAt:=xlPart, SearchOrder:=xlByColumns, _
MatchCase:=False, SearchFormat:=False,
ReplaceFormat:=False

Works fine, except that in order to have it perform the find-and-
replace on all worksheets in the workbook, I have to type CTRL-H and
change the "Within" property from "Sheet" to "Workbook". The next time
I open Excel, that property is automatically changed back to "Sheet."

Is there a way to set the "Within" property for find-and-replace in
VBA?

Thanks!

.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Find and Replace in VBA

Thanks, guys. That worked beautifully -- for a while. Now all of a
sudden I run the code and it does nothing. I haven't changed it at all
since it worked.
Anybody have any ideas? Here's the code:

Sub UpdateVariableInfo()
'Update all text that changes from school to school.
Dim ws As Worksheet

'Loop through each worksheet and run a find-and-replace to update
all variable data.
For Each ws In Sheets

'Find and replace all instances of the school name
FindAndReplace ws, "This", "That"

Next

End Sub

Sub FindAndReplace(ws As Worksheet, strFrom As String, strTo As
String)
'Run a find and replace operation on the designated worksheet.

ws.Cells.Replace What:=strFrom, Replacement:=strTo, _
LookAt:=xlPart, SearchOrder:=xlByColumns, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

End Sub



On Jan 26, 10:45*pm, Jacob Skaria
wrote:
Try the below which works for the entire workbook

Sub Macro()
Dim ws As Worksheet
For Each ws In Sheets
ws.Cells.Replace What:="This", Replacement:="That", _
LookAt:=xlPart, SearchOrder:=xlByColumns, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next
End Sub

--
Jacob

"septimus" wrote:
So I've got this code to perform a find-and-replace in an Excel
workbook:


* * * * Cells.Replace What:="This", Replacement:="That", _
* * * * * * * * LookAt:=xlPart, SearchOrder:=xlByColumns, _
* * * * * * * * MatchCase:=False, SearchFormat:=False,
ReplaceFormat:=False


Works fine, except that in order to have it perform the find-and-
replace on all worksheets in the workbook, I have to type CTRL-H and
change the "Within" property from "Sheet" to "Workbook". The next time
I open Excel, that property is automatically changed back to "Sheet."


Is there a way to set the "Within" property for find-and-replace in
VBA?


Thanks!


.


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/Replace Event or Find/Replace for Protected Sheet ... Joe HM Excel Programming 2 October 27th 07 03:55 PM
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


All times are GMT +1. The time now is 12:03 AM.

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"