Method: Replace for VBS
- Updated2024-09-12
- 2 minute(s) read
Methods > Method: Replace for VBS
Method: Replace for VBS
Replaces part of a text with another text.
vReplace = Object.Replace(expression, find, replacement, [start], [count], [compare])
| Object | VBS Object with this method. You do not need to specify this object. | ||||||||||
| expression | Variant Specifies the text you want to search through. | ||||||||||
| find | Variant Specifies the text to search for. | ||||||||||
| replacement | Variant Specifies the replacement text. | ||||||||||
| [start] | Variant Specifies the start position of the search. If you do not specify start, the search starts with the first character. If you specify count, you must also specify start. | ||||||||||
| [count] | Variant Specifies the number of replacements. If you do not specify count, the Replace method uses the value -1, which indicates that the method carries out all kinds of replacements. | ||||||||||
| [compare] | Variant Specifies the type of comparison when evaluating substrings. Possible settings are vbBinaryCompare and vbTextCompare. If you do not specify compare, the method performs a binary comparison. | ||||||||||
| vReplace | Variant Receives the replaced text. The new text starts with the start position. Possible return values:
|
The following example replaces various texts in a text:
MyResult = Replace("ABC123abc", "abc", "def") Call MsgBox(MyResult) ' Returns ABC123def MyResult = Replace("ABC123abc", "abc", "def", 1, -1, vbTextCompare) Call MsgBox(MyResult) ' Returns def123def MyResult = Replace("ABC123abc", "abc", "def",4) Call MsgBox(MyResult) ' Returns 123def