find replace macro

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top