If VBA's Replace function returns the original string unchanged, the cause is almost always one of four things: the search text is not actually present (extra spaces, wrong case in a case-sensitive context, or a non-breaking space), the function's return value was never assigned, the wrong argument order was used, or the code is reading a different string than you think. Fix it by printing the source string with Len and Asc to confirm its exact contents, assigning the result back to a variable or cell, and checking the argument order: Replace(expression, find, replace, start, count, compare). The sections below walk through each cause in the order that finds the fault fastest.
What You'll Need
- A working VBA project (Alt+F11 opens the editor in Excel, Word, Access and most Office hosts).
- The Immediate Window visible (Ctrl+G in the VBA editor) so you can test expressions line by line.
- A copy of your data or workbook, because debugging steps can overwrite cells.
- Option Explicit at the top of the module, so undeclared variables raise an error instead of silently returning Empty.
Step-by-Step
- Confirm the function is being called at all. Put a breakpoint (F9) on the Replace line and run the macro. If execution never reaches it, the problem is a loop condition, an If block that is false, or a procedure that is not the one being run. Use Debug > Step Into (F8) to walk through.
- Print the exact source string. In the Immediate Window type
? Len(Range("A1").Value)and? "[" & Range("A1").Value & "]". The brackets reveal leading and trailing spaces. If Len is larger than the visible text, hidden characters are present. - Check for non-breaking spaces and line breaks. A space copied from a web page is often Chr(160), not Chr(32), and Replace with a normal space will not match it. Test with
? Asc(Mid(Range("A1").Value, 1, 1))or loop through the characters and print their Asc values. Replace Chr(160) explicitly, or clean the string first withReplace(s, Chr(160), " "). - Verify the argument order. The signature is Replace(expression, find, replace, start, count, compare). A common mistake is swapping find and replace, or passing the compare argument in the start position. Name the arguments in the call (for example
Replace(expression:=s, find:="a", replace:="b")) to make the order self-documenting. - Assign the result. Replace does not modify the original string in place.
Replace(s, "a", "b")on its own line does nothing useful. Writes = Replace(s, "a", "b")or write the result directly to the cell:Range("A1").Value = Replace(Range("A1").Value, "a", "b"). - Check case sensitivity. The default compare mode is binary, so "Apple" will not match "apple". Pass vbTextCompare as the sixth argument:
Replace(s, "apple", "pear", 1, -1, vbTextCompare). Note that -1 in the count position means replace all occurrences. - Check the start and count arguments. A start value greater than 1 skips the beginning of the string. A count of 0 replaces nothing. Leave both out unless you need them, or pass 1 and -1 explicitly.
- Confirm the variable actually holds text. If the source is a Variant read from a cell that contains an error value or is empty, Replace may return the input unchanged or raise a type mismatch. Test with
? VarType(s)and guard withIf Not IsError(s) Then. - Check whether the code is writing back to the right place. If you replace into a variable but never assign it to the cell, the sheet looks unchanged. Step through and inspect the variable in the Locals window after the Replace line.
- Test the expression in isolation. Paste a minimal line into the Immediate Window, for example
? Replace("hello world", "world", "there"). If that works, the function is fine and the fault is in the data or the surrounding code.
Common Mistakes
- Forgetting the assignment. The single most common cause: calling Replace as a statement instead of using its return value.
- Swapping find and replace. The result looks like nothing happened because the code searched for the new text.
- Assuming Replace edits a cell. It works on strings only; you must read the cell, replace, and write back.
- Ignoring hidden characters. Non-breaking spaces, tabs and line breaks defeat an otherwise correct search string.
- Case mismatch. Binary comparison is the default, so mixed case silently fails.
- Using Replace on a range object.
Range("A1:A10").Replaceis a different method with different arguments (What, Replacement, LookAt, SearchOrder, MatchCase). Mixing the two up produces confusing results. - Missing Option Explicit. A misspelled variable name becomes an empty Variant, and Replace on Empty returns Empty.
Tips
- Use named arguments in complex calls so the order is obvious to anyone reading the code later.
- Build a small test harness: a fixed string, a known find value, and a Debug.Print of the result before touching real data.
- Clean input first: trim spaces, convert Chr(160) to a normal space, and strip Chr(10) and Chr(13) if line breaks are not wanted.
- For whole-cell substitutions across a column, the Range.Replace method is faster than looping cells with the string function, but remember its arguments differ.
- Keep a backup copy before running any bulk replace macro; there is no undo inside VBA.
- When the pattern is more complex than a literal string, use RegExp (Microsoft VBScript Regular Expressions) instead of chaining Replace calls.
Troubleshooting
If the Immediate Window test works but your macro does not, the fault is in the data path, not in Replace. Add Debug.Print lines immediately before and after the call to see the string going in and coming out. If the input looks correct but the output is unchanged, check the compare argument and the count argument again.
If Replace raises a type mismatch, the expression is not a string. Convert explicitly with CStr, after checking for errors and Empty. If the macro runs but the sheet does not change, confirm you are writing to the same sheet and range you read from, and that ScreenUpdating being off is not hiding the result (turn it back on or check the cell directly).
If nothing happens at all, confirm the procedure is in a standard module rather than a sheet or form module, and that the macro you are running is the one you edited. A second copy of the procedure with the same name in another module can shadow the one you are debugging.
If the replacement works for some rows and not others, sort the failures and inspect them individually. Mixed data types, trailing spaces and hidden characters usually explain the pattern.
Frequently Asked Questions
Why does Replace return the original string unchanged?
Either the find text is not present exactly as written (case, spaces, hidden characters), or the return value was never assigned. Check both before changing anything else.
Does VBA Replace change the original variable?
No. Strings are not modified in place; you must assign the result back, for example s = Replace(s, "a", "b").
How do I make Replace case-insensitive?
Pass vbTextCompare as the compare argument, the sixth parameter. The default binary comparison is case-sensitive.
What is the difference between Replace and Range.Replace?
Replace is a string function that returns a new string. Range.Replace is a worksheet method that edits cells directly and takes different arguments (What, Replacement, LookAt, SearchOrder, MatchCase).
Why does Replace fail on text copied from a website?
Web text often contains non-breaking spaces (Chr(160)) instead of normal spaces. Replace Chr(160) explicitly or clean the string first.
Can Replace handle multiple occurrences?
Yes. By default it replaces all occurrences; pass a count argument if you want to limit how many are replaced.