VBA REPLACE Function

VBA Replace Function in Excel

VBA Replace function in Excel is categorized as a Text/String function in VBA. It is a built-in function in MS Office Excel. It replaces a sub-string with another string in a given string. It has three required parameters and three optional parameters. If expression is Null, then the function returns an error. Expression contains length, then it returns an empty string.

This function use as a VBA function and a Excel Worksheet function(It has different syntax in Excel). The Replace function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA Replace function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the Replace function, where we can use this Replace function and real-time examples in VBA.

Syntax of VBA Replace Function

The syntax of the VBA Replace function is

Replace(Expression, Find, Replace, [Start], [Count], [Compare])

Note: This Replace function returns a string.

Parameters or Arguments:

This function has three mandatory parameters and three optional parameters for the Replace Function.
Where

Expression: An Expression is a mandatory argument. It represents a string expression you want to replace sub-string in.
Find: Find is a mandatory argument. It represents a sub-string which we want to find or search within an expression.
Replace: Replace is a mandatory argument. It represents a sub-string which we want to replace within an expression.
Start: Start is an optional parameter. Default value is ‘1’. It represents the position in expression to start search.
Count: Count is an optional parameter. It represents the number of occurrences to replace sub-string within an expression. Default value is ‘-1’. If we ignore, it will replace all occurrences of sub-string with another specified sub-string.
Compare: Compare is an optional parameter. It represents a numeric value. It specifies the type of comparison to evaluate the sub-strings. This argument can have anyone of the following value. Default comparison is ‘vbBinaryCompare’.

VBA Constant Value Description
vbUseCompareOption -1 Performs a comparison using the Option Compare statement.
vbBinaryCompare 0 performs a binary comparison
vbTextCompare 1 performs a text comparison
vbDatabaseCompare 2 performs a database comparison. It applies only in MS Access.

Where we can apply or use the VBA Replace Function?

We can use this VBA Replace function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Replace all occurrences of sub-string “F1” with “Help”

Here is a simple example of the VBA Replace function. This below example macro returns a string. The output of the below macro is ‘VBAHelp’.

'Replace all occurrences of sub-string "F1" with "Help".
Sub VBA_Replace_Function_Ex1()

    Dim sString As String, sSubString As String
    
    sString = "VBAF1"
    
    sSubString = Replace(sString, "F1", "Help")
    
    MsgBox "Replace F1 with Help :" & sSubString, vbInformation, "VBA Replace Function"
    
End Sub

Output: Here is the screen shot of the first example output.
VBA Replace Function

Example 2: Replace all occurrences of substring “Help” with “F1”

Here is a simple example of the VBA Replace function. This below example macro returns a string. The output of the below macro is ‘VBAF1’.

'Replace all occurrences of sub-string "Help" with "F1".
Sub VBA_Replace_Function_Ex2()

    Dim sString As String, sSubString As String
    
    sString = "VBAF1"
    
    sSubString = Replace(sString, "Help", "F1")
    
    MsgBox "Replace Help with F1 :" & sSubString, vbInformation, "VBA Replace Function"
    
End Sub

Output: Here is the screen shot of the second example output.
VBA Replace Function

Example 3: Replace all occurrences of sub-string “F1” with “Help” starting from position 13

Here is a simple example of the VBA Replace function. This below example macro returns a string. It ignores specified ‘N(start)’ characters, when we specify start position. The output of the below macro is ‘VBAHelp-VBAHelp-VBAHelp’.

'Replace all occurrences of sub-string "F1" with "Help" starting from position 13
Sub VBA_Replace_Function_Ex3()

    Dim sString As String, sSubString As String
    
    sString = "VBAF1-VBAF1-VBAF1-VBAF1-VBAF1"
    
    sSubString = Replace(sString, "F1", "Help", 13)
    
    MsgBox "Replace F1 with Help :" & sSubString, vbInformation, "VBA Replace Function"
    
End Sub

Output: Here is the screen shot of the third example output.
VBA Replace Function

Example 4: Replace all occurrences of sub-string “F1” with “Help” starting from position 13 and keep whole string

Here is a simple example of the VBA Replace function. This below example macro returns a string. It ignores specified ’13(start)’ characters, when we specify start position. In this example we are using left function to extract left most characters from the given string. And adding the output to original output. Here is the final output of the below macro is ‘VBAF1-VBAF1-VBAHelp-VBAHelp-VBAHelp’.

'Replace all occurrences of sub-string "F1" with "Help" starting from position 13 and keep whole string
Sub VBA_Replace_Function_Ex4()

    Dim sString As String, sSubString As String
    
    sString = "VBAF1-VBAF1-VBAF1-VBAF1-VBAF1"
    
    sSubString = Left(sString, 12) & Replace(sString, "F1", "Help", 13)
    
    MsgBox "Replace F1 with Help :" & sSubString, vbInformation, "VBA Replace Function"
    
End Sub

Output: Here is the screen shot of the fourth example output.
VBA Replace Function

Example 5: Replace last occurrence of sub-string “F1” with “Help”

Here is a simple example of the VBA Replace function. This below example macro returns a string. Here is the final output of the below macro is ‘VBAF1-VBAF1-VBAF1-VBAF1-VBAHelp’.

'Replace last occurrence of substring "F1" with "Help"
Sub VBA_Replace_Function_Ex5()

    Dim sString As String, sSubString As String
    
    sString = "VBAF1-VBAF1-VBAF1-VBAF1-VBAF1"
    
    sString = StrReverse(sString)
    
    sString = Replace(sString, StrReverse("F1"), StrReverse("Help"), , 1)
    
    sSubString = StrReverse(sString)
    
    MsgBox "Replace F1 with Help :" & sSubString, vbInformation, "VBA Replace Function"
    
End Sub

Output: Here is the screen shot of the fifth example output.
VBA Replace Function

Example 6: Remove all occurrences of sub-string ‘F1’

Here is a simple example of the VBA Replace function. This below example macro returns a string. It removes all occurrences of sub-string within a string. Here is the final output of the below macro is ‘VBA-VBA-VBA-VBA-VBA’.

'Remove all occurrences of sub-string 'F1'
Sub VBA_Replace_Function_Ex6()

    Dim sString As String, sSubString As String
    
    sString = "VBAF1-VBAF1-VBAF1-VBAF1-VBAF1"
    
    sSubString = Replace(sString, "F1", "")
    
    MsgBox "Remove all occurences of F1 :" & sSubString, vbInformation, "VBA Replace Function"
    
End Sub

Output: Here is the screen shot of the sixth example output.
VBA Replace Function

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers

3 thoughts on “VBA Replace Function in Excel”

Leave a Reply