MS Excel: REPLACE Function (VBA)
Learn how to use the Excel REPLACE function (in VBA) with syntax and examples.
The Microsoft Excel REPLACE function replaces a sequence of characters in a string with another set of characters. Please note that the worksheet version of the REPLACE function has different syntax.
The syntax for the Microsoft Excel REPLACE function is:
Replace ( string1, find, replacement, [start, [count, [compare]]] )
Parameters or Arguments
string1 is the string to replace a sequence of characters with another set of characters.
find is the string that will be searched for in string1.
replacement will replace find in string1.
start is optional. This is the position in string1 to begin the search. If this parameter is omitted, the REPLACE function will begin the search at position 1.
count is optional. This is the number of occurrences to replace. If this parameter is omitted, the REPLACE function will replace all occurrences of find with replacement.
compare is optional. This can be one of the following values:
|vbDatabaseCompare||Performs a comparison based on information in your database|
The REPLACE function can be used in the following versions of Microsoft Excel:
- Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000
Type of Excel Function
The REPLACE function can be used in Microsoft Excel as the following type of function:
- VBA function (VBA)
Example (as VBA Function)
The REPLACE function can be used in VBA code in Microsoft Excel.
Let's look at some Excel REPLACE function examples and explore how you would use the REPLACE function in Excel VBA code:
|Replace("alphabet", "bet", "hydro")||would return "alphahydro"|
|Replace ("alphabet", "a", "e")||would return "elphebet"|
|Replace("alphabet", "a", "e", 2)||would return "lphebet"|
|Replace("alphabet", "a", "e", 1, 1)||would return "elphabet"|
im LResult As String LResult = Replace("alphabet", "a", "e")
In this example, the variable called LResult would now contain the value "elphebet".