Search and replace vb code

alokie

New Member
Joined
Sep 6, 2011
Messages
5
Help... I am trying to create a button that will search for the item in field Updates!a4 and replace it with the text in Updates!c4... I need this to search through the entire workbook.. any help would be great.. I am about a green as it comes with this stuff..

Sub replace()
What = Updates!a4
repl = Updates!c4
Sheets().Select

Selection.replace What:=What, Replacement:=repl, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub

Error - Object required (Error 424)
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
One way:
Code:
Sub Replace()
    Dim wks         As Worksheet
    Dim vWhat       As Variant
    Dim vRepl       As Variant
 
    vWhat = Worksheets("Updates").Range("A4").Value
    vRepl = Worksheets("Updates").Range("C4").Value
 
    For Each wks In Worksheets
        wks.Cells.Replace What:=vWhat, _
                          Replacement:=vRepl, _
                          LookAt:=xlPart, _
                          SearchOrder:=xlByRows, _
                          MatchCase:=False, _
                          SearchFormat:=False, _
                          ReplaceFormat:=False
    Next wks
End Sub
 
Upvote 0
You're welcome.

You should also include the LookIn argument (xlValues or xlFormulas)
 
Upvote 0
If I were to only need to search a specific area in a different sheet. Would I just add...

With Range("A1:A10")
Set LastCell = .Cells(.Cells.Count)
End With
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top