Table of Contents
The Substitute function in VBA allows for the replacement of specific characters or strings within a given text or cell. This function can be useful for data manipulation and formatting within Excel spreadsheets. To use the Substitute function, the user must specify the text or cell to be modified, the text or characters to be replaced, and the new text or characters to be inserted in place. For example, the following formula would replace all instances of the letter “a” in cell A1 with the letter “b”: =SUBSTITUTE(A1, “a”, “b”)
Use Substitute in VBA (With Example)
You can use the Substitute() method in VBA to replace all occurrences of specific text in a string with new text.
This method uses the following basic syntax:
Substitute(Text, Old_text, New_text, Instance_num)
where:
- Text: The text you want to substitute characters in
- Old_text: The text to replace
- New_text: The text to use as replacement
- Instance_num (optional): Which occurrence of old text to replace. If none is specified, then all occurrences of old text are replaced.
The following examples shows how to use this method in practice.
Example: How to Use Substitute in VBA
Suppose we have the following list of phrases in Excel:

Suppose that we would like to replace each space in each cell with a comma instead.
We can create the following macro to do so:
Sub SubstituteText()
Dim rng As Range, cell As Range
Set rng = Range("A2:A9")
For Each cell In rng
cell = WorksheetFunction.Substitute(cell, " ", ",")
NextEnd Sub
When we run this macro, we receive the following output:

Notice that each space in each cell of column A has been replaced with a comma.
Note that we could also use the following syntax to replace each space with nothing:
Sub SubstituteText()
Dim rng As Range, cell As Range
Set rng = Range("A2:A9")
For Each cell In rng
cell = WorksheetFunction.Substitute(cell, " ", "")
NextEnd Sub

Notice that each space in each cell of column A has been replaced with nothing.
In other words, we have removed each of the spaces from each cell in column A.
Note: You can find the complete documentation for the VBA Substitute method .
The following tutorials explain how to perform other common tasks using VBA:
Cite this article
stats writer (2024). How can I use the Substitute function in VBA?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-substitute-function-in-vba-and-could-you-provide-an-example/
stats writer. "How can I use the Substitute function in VBA?." PSYCHOLOGICAL SCALES, 21 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-substitute-function-in-vba-and-could-you-provide-an-example/.
stats writer. "How can I use the Substitute function in VBA?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-substitute-function-in-vba-and-could-you-provide-an-example/.
stats writer (2024) 'How can I use the Substitute function in VBA?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-substitute-function-in-vba-and-could-you-provide-an-example/.
[1] stats writer, "How can I use the Substitute function in VBA?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use the Substitute function in VBA?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
