WorksheetFunction.Replace method (Excel)
Replaces part of a text string, based on the number of characters that you specify, with a different text string.
Syntax
expression.Replace (Arg1, Arg2, Arg3, Arg4)
expression A variable that represents a WorksheetFunction object.
Parameters
Name | Required/Optional | Data type | Description |
---|---|---|---|
Arg1 | Required | String | Text in which you want to replace some characters. |
Arg2 | Required | Double | The position of the character in Arg1 that you want to replace with Arg4. |
Arg3 | Required | Double | The number of characters in Arg1 that you want the Replace method to replace with Arg4. |
Arg4 | Required | String | Text that will replace characters in Arg1. |
Return value
A String value that represents the new string, after replacement.
Example
This example replaces abcdef with ac-ef and notifies the user during this process.
Sub UseReplace()
Dim strCurrent As String
Dim strReplaced As String
strCurrent = "abcdef"
' Notify user and display current string.
MsgBox "The current string is: " & strCurrent
' Replace "cd" with "-".
strReplaced = Application.WorksheetFunction.Replace _
(Arg1:=strCurrent, Arg2:=3, _
Arg3:=2, Arg4:="-")
' Notify user and display replaced string.
MsgBox "The replaced string is: " & strReplaced
End Sub
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.