VBA Find and Replace (whole workbook)

c0087

Board Regular
Joined
Jul 13, 2015
Messages
94
Office Version
  1. 365
Platform
  1. Windows
[TABLE="width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]A Anderson[/TD]
[TD]Alex Anderson[/TD]
[/TR]
[TR]
[TD]B Smith[/TD]
[TD]Brandon Smith[/TD]
[/TR]
</tbody>[/TABLE]



- I pasted my list of names in sheet 1.
- I need to FiIND A1 (entire workbook) and replace all instances with B1.
- then same thing for A2 to B2 and so on so forth.

Thanks in advance
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Is that EVERY sheet in the workbook, or might there be exeptions ?
 
Upvote 0
Is that EVERY sheet in the workbook, or might there be exeptions ?

YES i need it to search the entire workbook for each individual search. NO there won't be any exceptions




*The process i would be doing manually is
typing A1 in the FIND
typing B1 in the REPLACE
selecting FIND ALL
selecting REPLACE ALL

Within : Workbook
Search by: Columns
Search in: Forumulas

Match case is selected
 
Last edited:
Upvote 0
The following code assumes that the workbook of interest is the active workbook, and it searches all sheets except Sheet1. If you want Sheet1 to be searched as well, the code will need to be amended accordingly. Also, it matches the partial cell contents. To match the entire cell contents instead, replace xlPart with xlWhole.

Code:
Sub WorkbookFindAndReplace()

    Dim sourceWorksheet As Worksheet
    Dim currentWorksheet As Worksheet
    Dim findAndReplaceRange As Range
    Dim searchFor As String
    Dim replaceWith As String
    Dim lastRow As Long
    Dim i As Long
    
    On Error Resume Next
    Set sourceWorksheet = ActiveWorkbook.Worksheets("Sheet1")
    If sourceWorksheet Is Nothing Then
        MsgBox "The source worksheet was not found!", vbExclamation
        Exit Sub
    End If
    On Error GoTo 0
    
    With sourceWorksheet
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set findAndReplaceRange = .Range("A1:B" & lastRow)
    End With
    
    For i = 1 To findAndReplaceRange.Rows.Count
        searchFor = findAndReplaceRange(i, 1).Value
        If Len(searchFor) > 0 Then
            replaceWith = findAndReplaceRange(i, 2).Value
            For Each currentWorksheet In ActiveWorkbook.Worksheets
                If currentWorksheet.Name <> sourceWorksheet.Name Then
                    WorksheetFindAndReplace currentWorksheet, searchFor, replaceWith
                End If
            Next currentWorksheet
        End If
    Next i
    
End Sub


Sub WorksheetFindAndReplace(ByVal currentWorksheet As Worksheet, ByVal searchFor As String, ByVal replaceWith As String)

    Dim foundCell As Range

    With currentWorksheet.Cells
        Set foundCell = .Find(What:=searchFor, LookIn:=xlFormulas, LookAt:=xlPart, SearchDirection:=xlNext, MatchCase:=True)
        If Not foundCell Is Nothing Then
            Do
                foundCell.Value = replaceWith
                Set foundCell = .FindNext(foundCell)
            Loop Until foundCell Is Nothing
        End If
    End With
    
End Sub

Hope this helps!
 
Last edited:
Upvote 0
The following code assumes that the workbook of interest is the active workbook, and it searches all sheets except Sheet1. If you want Sheet1 to be searched as well, the code will need to be amended accordingly. Also, it matches the partial cell contents. To match the entire cell contents instead, replace xlPart with xlWhole.

Code:
Sub WorkbookFindAndReplace()

    Dim sourceWorksheet As Worksheet
    Dim currentWorksheet As Worksheet
    Dim findAndReplaceRange As Range
    Dim searchFor As String
    Dim replaceWith As String
    Dim lastRow As Long
    Dim i As Long
    
    On Error Resume Next
    Set sourceWorksheet = ActiveWorkbook.Worksheets("Sheet1")
    If sourceWorksheet Is Nothing Then
        MsgBox "The source worksheet was not found!", vbExclamation
        Exit Sub
    End If
    On Error GoTo 0
    
    With sourceWorksheet
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set findAndReplaceRange = .Range("A1:B" & lastRow)
    End With
    
    For i = 1 To findAndReplaceRange.Rows.Count
        searchFor = findAndReplaceRange(i, 1).Value
        If Len(searchFor) > 0 Then
            replaceWith = findAndReplaceRange(i, 2).Value
            For Each currentWorksheet In ActiveWorkbook.Worksheets
                If currentWorksheet.Name <> sourceWorksheet.Name Then
                    WorksheetFindAndReplace currentWorksheet, searchFor, replaceWith
                End If
            Next currentWorksheet
        End If
    Next i
    
End Sub


Sub WorksheetFindAndReplace(ByVal currentWorksheet As Worksheet, ByVal searchFor As String, ByVal replaceWith As String)

    Dim foundCell As Range

    With currentWorksheet.Cells
        Set foundCell = .Find(What:=searchFor, LookIn:=xlFormulas, LookAt:=xlPart, SearchDirection:=xlNext, MatchCase:=True)
        If Not foundCell Is Nothing Then
            Do
                foundCell.Value = replaceWith
                Set foundCell = .FindNext(foundCell)
            Loop Until foundCell Is Nothing
        End If
    End With
    
End Sub

Hope this helps!

Will give it a go and report back. Thanks
 
Upvote 0
Could also try....

Code:
Sub MM1()
Dim ws As Worksheet, r As Long, lr As Long, sh As Worksheet
Set sh = Sheets("Sheet1")
lr = sh.Cells(Rows.Count, "A").End(xlUp).Row
    For Each ws In Worksheets
        If ws.Name <> "Sheet1" Then
            ws.Activate
                For r = 1 To lr
                    ActiveSheet.UsedRange.Replace sh.Range("A" & r).Value, sh.Range("B" & r).Value, xlWhole, , False, , False, False
                Next r
        End If
    Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,108
Members
452,544
Latest member
aush

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