The Excel VBA Replace function swaps one substring for another inside a text string and returns the new string. It does not change the original cell unless you write the result back. The shortest working pattern is: newText = Replace(oldText, "find", "replace"). This guide shows how to use it safely, including the optional start position and count arguments, and how to apply it across a range of cells.

What You'll Need

  • Excel on Windows or Mac with macros enabled (the Developer tab visible, or access to the Visual Basic Editor via Alt+F11 on Windows; on Mac use the Developer tab or Tools > Macro > Visual Basic Editor).
  • A workbook saved as .xlsm if you want to keep your macro.
  • Basic familiarity with opening the VBA Editor and inserting a module.

Menu labels and the exact location of the Developer tab can differ slightly by Excel version, platform and region.

Step-by-Step

  1. Open the VBA Editor. Press Alt+F11 on Windows. On Mac, open Tools > Macro > Visual Basic Editor from the Excel menu, or use the Developer tab if it is enabled. The editor opens in a separate window.
  2. Insert a module. In the editor, choose Insert > Module. A blank code pane appears. This is where your macro will live.
  3. Write a simple Replace statement. Type the following into the module:
    Sub SimpleReplace()
        Dim result As String
        result = Replace("Hello World", "World", "Excel")
        MsgBox result
    End Sub
    This returns "Hello Excel". The first argument is the text to search, the second is what to find, the third is what to substitute.
  4. Run the macro. Place the cursor inside the Sub and press F5, or choose Run > Run Sub/UserForm. A message box shows the result. If you see a compile error, check that you have not mistyped the function name or missed a parenthesis.
  5. Use the optional start argument. Replace accepts a fourth argument, Start, which is the character position to begin searching. For example, Replace("aaa", "a", "b", 2) returns "abb" because it starts at the second character. The default is 1.
  6. Use the optional count argument. The fifth argument, Count, limits how many replacements are made. Replace("aaa", "a", "b", 1, 1) returns "baa". The default is -1, which means replace all occurrences.
  7. Control case sensitivity. The sixth argument, Compare, can be vbBinaryCompare (case-sensitive, the default) or vbTextCompare (case-insensitive). Replace("Hello", "hello", "Hi", 1, -1, vbTextCompare) returns "Hi".
  8. Apply Replace to a cell. To change the value of cell A1 in place, use:
    Range("A1").Value = Replace(Range("A1").Value, "old", "new")
    This reads the cell, replaces the text, and writes it back.
  9. Loop through a range. To replace across many cells, loop over each cell:
    Sub ReplaceInRange()
        Dim c As Range
        For Each c In Range("A1:A10")
            If Not IsEmpty(c.Value) Then
                c.Value = Replace(c.Value, "find", "replace")
            End If
        Next c
    End Sub
    The IsEmpty check avoids errors on blank cells.
  10. Test on a copy first. Save a duplicate of your workbook before running a macro that writes back to cells. Replace cannot be undone with Ctrl+Z after a macro runs, so a backup is your safety net.

Common Mistakes

  • Forgetting that Replace returns a string. It does not modify the original variable or cell. You must assign the result: x = Replace(x, ...) or Range("A1").Value = Replace(...).
  • Assuming case-insensitive matching. By default Replace is case-sensitive. Use vbTextCompare when you want "Hello" and "hello" treated the same.
  • Passing a non-string value. If a cell contains a number or date, Replace converts it to text. The result may lose formatting. Convert back or handle numbers separately.
  • Mixing up argument order. The order is Expression, Find, Replace, Start, Count, Compare. Swapping Start and Count gives unexpected results.
  • Using Replace on a blank cell. A blank cell returns an empty string; replacing inside it does nothing but can overwrite formulas if you are not careful. Check IsEmpty first.
  • Expecting Replace to work on multiple cells at once. Replace works on a single string. You need a loop for ranges.

Tips

  • Use a variable for the find and replace text so you can change them in one place.
  • Combine Replace with Trim or LCase to clean data before or after substitution.
  • If you need to replace line breaks, use Chr(10) or vbLf as the find string.
  • For large ranges, turn off ScreenUpdating at the start of the macro and turn it back on at the end to speed things up.
  • Test with a small range first, then expand.
  • Remember that Replace is a VBA function, not the same as Excel's Find and Replace dialog (Ctrl+H). The dialog changes cells directly; the VBA function returns a string.

Troubleshooting

If your macro does not compile, check for a missing parenthesis, a misspelled function name, or a missing End Sub. If it runs but nothing changes, confirm you assigned the result back to the cell or variable. If you get a type mismatch, the cell may contain an error value; test with IsError first. If replacements happen in the wrong place, review the Start and Count arguments. If case sensitivity is wrong, add vbTextCompare. If the macro is slow on a large range, disable ScreenUpdating and Calculation temporarily, then re-enable them. If you accidentally overwrite data, close without saving and reopen your backup; macros cannot be undone with Ctrl+Z.

Frequently Asked Questions

Does VBA Replace change the original cell?

No. It returns a new string. You must write the result back to the cell or variable.

Is VBA Replace case-sensitive?

Yes, by default. Pass vbTextCompare as the sixth argument for case-insensitive matching.

Can Replace handle multiple occurrences?

Yes. By default it replaces all occurrences. Use the Count argument to limit how many are replaced.

What is the difference between Replace and Excel's Find and Replace?

Excel's Find and Replace (Ctrl+H) edits cells directly through the interface. VBA Replace is a function that works on a string and returns a new string; you control where the result goes.

Can I use Replace on a range without a loop?

No. Replace works on one string at a time. Loop through cells to apply it across a range.

What happens if the find text is not present?

Replace returns the original string unchanged. No error is raised.