NamedRange.Replace Method (Object, Object, Object, Object, Object, Object, Object, Object)


Replaces the specified characters in the NamedRange control with a new string.

Namespace:   Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel (in Microsoft.Office.Tools.Excel.dll)

bool Replace(
	object What,
	object Replacement,
	object LookAt,
	object SearchOrder,
	object MatchCase,
	object MatchByte,
	object SearchFormat,
	object ReplaceFormat


Type: System.Object

The string you want Microsoft Office Excel to search for.

Type: System.Object

The replacement string.

Type: System.Object

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

Type: System.Object

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

Type: System.Object

true to make the search case sensitive.

Type: System.Object

You can use this argument only if you have selected or installed double-byte language support in Excel. true to have double-byte characters match only double-byte characters; false to have double-byte characters match their single-byte equivalents.

Type: System.Object

The search format for the method.

Type: System.Object

The replace format for the method.

Return Value

Type: System.Boolean

true if the specified characters are in cells within the NamedRange control; otherwise, false.

Using this method does not change either the selection or the active cell.

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, set these arguments explicitly each time you use this method.

For information on optional parameters, see Optional Parameters in Office Solutions.

The following code example sets the value of the cells in a NamedRange control to the string "This is a sentence.", and then uses the Replace method to replace the substring "a" with "my".

This example is for a document-level customization.

private void ReplaceValue()
    Microsoft.Office.Tools.Excel.NamedRange namedRange1 =

    namedRange1.Value2 = "This is a sentence.";
    namedRange1.Replace("a", "my", Excel.XlLookAt.xlPart,
        Excel.XlSearchOrder.xlByColumns, false);
Return to top