Replace Method

Microsoft Excel Visual Basic

Returns a Boolean indicating characters in cells within the specified range. Using this method doesn’t change either the selection or the active cell.

expression.Replace(What, Replacement, LookAt, SearchOrder, MatchCase, MatchByte, SearchFormat, ReplaceFormat)

expression    Required. An expression that returns a Range object.

What   Required Variant. The string you want Microsoft Excel to search for.

Replacement   Required Variant. The replacement string.

LookAt    Optional Variant. Can be one of the following XlLookAt constants: xlWhole or xlPart.

SearchOrder    Optional Variant. Can be one of the following XlSearchOrder constants: xlByRows or xlByColumns.

MatchCase    Optional Variant. True to make the search case sensitive.

MatchByte    Optional Variant. You can use this argument only if you’ve selected or installed double-byte language support in Microsoft Excel. True to have double-byte characters match only double-byte characters. False to have double-byte characters match their single-byte equivalents.

SearchFormat   Optional Variant. The search format for the method.

ReplaceFormat   Optional Variant. The replace format for the method.

Remarks

The settings for LookAt, SearchOrder, MatchCase, and MatchByte are saved each time you use this method. If you don’t specify values for these arguments the next time you call the method, the saved values are used. Setting these arguments changes the settings in the Find dialog box, and changing the settings in the Find dialog box changes the saved values that are used if you omit the arguments. To avoid problems, set these arguments explicitly each time you use this method.

ShowReplace method as it applies to the WorksheetFunction object.

Replaces part of a text string, based on the number of characters you specify, with a different text string.

expression.Replace(Arg1, Arg2, Arg3, Arg4)

expression    Required. An expression that returns a WorksheetFunction object.

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.

Example

ShowAs it applies to the Range object.

This example replaces every occurrence of the trigonometric function SIN with the function COS. The replacement range is column A on Sheet1.

Worksheets("Sheet1").Columns("A").Replace _
    What:="SIN", Replacement:="COS", _
    SearchOrder:=xlByColumns, MatchCase:=True
				

ShowAs it applies to the WorksheetFunction object.

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