ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   find replace macro (https://www.excelbanter.com/excel-programming/319203-find-replace-macro.html)

Todd L.

find replace macro
 
I have a macro that finds and replaces (I found it here, thanks!) I need to
adapt it from looping through all worksheets to just cells I select. Can
someone help?


TIA


Todd




Sub FindReplace()


Dim wks As Worksheet
Dim WhatToReplace As String
Dim WithWhat As String


WhatToReplace = InputBox(Prompt:="What this time?")
If WhatToReplace = "" Then
Exit Sub
End If



WithWhat = InputBox(Prompt:="what should: " & Chr(34) & _
WhatToReplace & Chr(34) & " be replaced with?")
If WithWhat = "" Then
Exit Sub
End If

For Each wks In ActiveWorkbook.Worksheets
wks.Cells.Replace what:=WhatToReplace, _
Replacement:=WithWhat, lookat:=xlPart, _
MatchCase:=False
Next wks


End Sub


Bernie Deitrick

find replace macro
 
Todd,

Replace

wks.Cells.Replace ...

with either an address, like

wks.Range("A1:B10").Replace ....

or to act on the current selection from each sheet

wks.Select
Selection.Replace ....

HTH,
Bernie
MS Excel MVP

"Todd L." wrote in message
...
I have a macro that finds and replaces (I found it here, thanks!) I need

to
adapt it from looping through all worksheets to just cells I select. Can
someone help?


TIA


Todd




Sub FindReplace()


Dim wks As Worksheet
Dim WhatToReplace As String
Dim WithWhat As String


WhatToReplace = InputBox(Prompt:="What this time?")
If WhatToReplace = "" Then
Exit Sub
End If



WithWhat = InputBox(Prompt:="what should: " & Chr(34) & _
WhatToReplace & Chr(34) & " be replaced with?")
If WithWhat = "" Then
Exit Sub
End If

For Each wks In ActiveWorkbook.Worksheets
wks.Cells.Replace what:=WhatToReplace, _
Replacement:=WithWhat, lookat:=xlPart, _
MatchCase:=False
Next wks


End Sub





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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com