Replace Method

Applies To

Range Object.

Description

Finds and replaces characters in cells within a range. Does not change the selection or active cell.

For help about using the Replace worksheet function in Visual Basic, see "Using Worksheet Functions in Visual Basic" in online Help.

Syntax

object.Replace(what, replacement, lookAt, searchOrder, matchCase, matchByte)

object

Required. Replace characters or cells in this range.

what

Required. A string indicating the contents for which you want to search.

replacement

Required. A string indicating the text with which you want to replace what.

lookAt

Optional. If xlWhole, what must match the entire contents of a cell. If xlPart, what must contain part of the contents of a cell.

searchOrder

Optional. Specifies whether to search by rows (xlByRows) or columns (xlByColumns).

matchCase

Optional. If True, search is case sensitive.

matchByte

Optional. Used only in Far East Microsoft Excel. If True, double-byte characters match only double-byte characters. If False, double-byte characters can match their single-byte equivalents.

Remarks

The settings for lookAt, searchOrder, matchCase, and matchByte are saved each time you use this method. If you do not 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, explicitly set these arguments each time you use this method.

If the contents of the what argument are found in at least one cell of the sheet, the 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