Replace Method

Applies To

Range object.

Description

Finds and replaces characters in cells within a range. Doesn't change the selection or the active cell.

Syntax

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

expression Required. An expression that returns a Range object.

What Required String. The string to search for.

Replacement Required String. 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. Used only in Far East versions of Microsoft Excel. True to have double-byte characters match only double-byte characters. False to have double-byte characters match their single-byte equivalents.

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.

If the contents of the What argument are found in at least one cell on the sheet, this method returns True.

See Also

Find method, FindNext method.

Example

This example replaces every occurrence of the 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