VBA Find and Replace in All Workbook

SammiBoi

Board Regular
Joined
Jun 1, 2007
Messages
77
I wanted to find and replace the word AAA with the word BBB on all worksheets in my workbook.

Should I use the "Selection" arguement or should I be using something else?

<Code>
Selection.Replace What:="AAA", Replacement:="BBB", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
</Code>

Thanks
Sam
 
Last edited:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
This seemed to work for me:

Code:
Cells.Replace What:="AAA", Replacement:="BBB", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
 
Upvote 0
This seemed to work for me:

Code:
Cells.Replace What:="AAA", Replacement:="BBB", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

that seem to only work for me on the active worksheet but not on the rest of the worksheets....
 
Upvote 0
Try:

Code:
Sub FindReplaceAll()
Dim ws      As Worksheet
For Each ws In ActiveWorkbook.Worksheets
   ws.Cells.Replace What:="AAA", Replacement:="BBB", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Next ws
 
Upvote 0
That's interesting. I changed the option to search from "Worksheet" to "Workbook" and that was the generated code.

Try:

Code:
Sub test()
Dim ws As Worksheet
For Each ws In Worksheets
    With ws
        .Cells.Replace What:="AAA", Replacement:="BBB", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    End With
Next ws
End Sub
 
Upvote 0
You don't need to loop through all worksheets. I thought I had to but then realized it was something to do with the other arguments in the replace method. Cells applies to the active workbook. Here is my routine that worked for me (goes through a list of cells performing several (Replace All' iterations)..

Sub f_and_r() 'routine for making a series of workbook find+replace changes


'place cursor here and press F8 repeatedly to step through the code, open excel sheet in a seperate window to see changes being made as you execute each line


Dim i, j, k As Integer
Dim sht As Worksheet
Dim bk As Workbook
Dim what1, replace1 As String


Set bk = ThisWorkbook
Set sht = ThisWorkbook.Sheets("work")
For i = 1111 To 1124 'rows to loop through
what1 = sht.Range("a" & i) 'the string to find
replace1 = Left(Range("a" & i), 4) & "blog" & Mid(Range("a" & i), 9, 10) 'the replacement for the find string
Debug.Print what1, replace1 'just a quick reference check to make sure what and if are correct
Cells.Replace what1, replace1 'executing the replace method on the whole workbook
Next i


End Sub

Thanks,

Craig
Analyze6 - Applied Data Analysis Techniques
 
Upvote 0
I'm having a problem in macro coding in my Dataset
When i try to find "AAA" and replace with "BBB"
it also find and replace "AAACC" with "BBBCC"

any solution is appreciated
 
Upvote 0
Hi There--in Excel you can do this using 'match entire cell contents' when doing a search and replace. I just used the macro recorder and here is the code it spits out (below). You will notice the LookAt argument = xlWhole. So just use that as an argument in the cells.replace method.. like Cells.Replace "AAA", "CCC", LookAt:=xlWhole... hopefully that should help you solve it.

Cheers,

Craig


Sub Macro1()
Cells.Replace What:="AAA", Replacement:="CCC", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,122
Members
451,399
Latest member
alchavar

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