VBA macros · Data

VBA macro to find and replace across all sheets in Excel

Find and replace text in every worksheet of a workbook at once, including protected-sheet checks, wildcard notes and a match-whole-cell option.

Windows + Mac

Excel's Find and Replace dialog can search the whole workbook when you change Within to Workbook, but many people never notice that option, and it does not remember it between sessions. When a product is renamed, a department code changes or a year has to be updated everywhere, this macro asks for the two texts and replaces them on every unprotected sheet of the active workbook.

VBA
Option Explicit

Sub FindReplaceAllSheets()
    Dim ws As Worksheet
    Dim findText As Variant, replaceText As Variant
    Dim skipped As String

    findText = Application.InputBox("Find what:", "Replace in all sheets", Type:=2)
    If VarType(findText) = vbBoolean Then Exit Sub
    If Len(findText) = 0 Then Exit Sub

    replaceText = Application.InputBox("Replace '" & findText & "' with:", _
                                       "Replace in all sheets", Type:=2)
    If VarType(replaceText) = vbBoolean Then Exit Sub

    Application.ScreenUpdating = False

    For Each ws In ActiveWorkbook.Worksheets
        If ws.ProtectContents Then
            skipped = skipped & vbLf & ws.Name
        Else
            ws.Cells.Replace What:=findText, Replacement:=replaceText, _
                LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
                SearchFormat:=False, ReplaceFormat:=False
        End If
    Next ws

    Application.ScreenUpdating = True

    If Len(skipped) > 0 Then
        MsgBox "Done. Protected sheets skipped:" & skipped, vbExclamation
    Else
        MsgBox "Done.", vbInformation
    End If
End Sub
Replace text on every worksheet of the active workbook

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick FindReplaceAllSheets and click Run.
  5. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.

How it works

Both texts are asked for with Application.InputBox, which returns False when you click Cancel; the VarType checks stop the macro in that case. An empty find text is rejected, because replacing nothing would make no sense, while an empty replace text is allowed and simply deletes the found text.

ws.Cells.Replace is the VBA version of the Replace dialog for one sheet. LookAt:=xlPart replaces the text wherever it occurs inside a cell; MatchCase:=False ignores upper and lower case. SearchFormat and ReplaceFormat are set to False so formats chosen earlier in the dialog do not sneak in.

Replace works on what is stored in the cell. For a formula that is the formula text, not the result, so replacing Sheet1 with Data also rewrites references inside formulas. Protected sheets are skipped and listed, rather than causing an error halfway through.

Change it

To replace only when the whole cell matches, so North is replaced but North-East is not, use LookAt:=xlWhole. For a case-sensitive replace, set MatchCase:=True.

To run a whole list of replacements in one go, put the old values in column A and the new values in column B of a sheet called Replace list and loop through it:

VBA
Sub ReplaceFromList()
    Dim lst As Worksheet, ws As Worksheet
    Dim r As Long
    Set lst = ActiveWorkbook.Worksheets("Replace list")
    Application.ScreenUpdating = False
    For r = 2 To lst.Cells(lst.Rows.Count, 1).End(xlUp).Row
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name <> lst.Name And Not ws.ProtectContents Then
                ws.Cells.Replace What:=lst.Cells(r, 1).Text, Replacement:=lst.Cells(r, 2).Text, _
                    LookAt:=xlWhole, MatchCase:=False
            End If
        Next ws
    Next r
    Application.ScreenUpdating = True
End Sub
Replace many values from a two-column list

The list sheet is skipped so the list itself is not changed by the replacements.

What can go wrong

Watch out. * and ? are wildcards in Replace: * matches any text and ? any single character. Searching for * alone would replace the entire content of every non-empty cell. To find a real asterisk or question mark, type a tilde in front of it: ~* or ~?. A macro cannot be undone with Ctrl+Z (Mac: Cmd+Z). Save the workbook, or make a copy with VBA macro to save a backup copy with a timestamp in Excel, before you run it on real data.

Replace also changes the settings of the Find and Replace dialog, so the next time you press Ctrl+H the options may differ from what you expect. Excel tables, charts' titles and text boxes are not searched; only cell contents are. Values in cells formatted as numbers are matched against the stored value, not the displayed one.

Tip. Works on Windows and Mac. To find without replacing, use Ctrl+F with Within: Workbook, or see Find, filter and sort data with VBA for searching with code.

Related: VBA macro to trim spaces in selected cells in Excel, Find, filter and sort data with VBA.

More vba macros