Export (0) Print
Expand All
Expand Minimize

NamedRange.Find Method

Finds specific information in the NamedRange control, and returns a Microsoft.Office.Interop.Excel.Range object that represents the first cell where that information is found.

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

Range Find(
	Object What,
	Object After,
	Object LookIn,
	Object LookAt,
	Object SearchOrder,
	XlSearchDirection SearchDirection,
	Object MatchCase,
	Object MatchByte,
	Object SearchFormat
)

Parameters

What
Type: System.Object

The data to search for. Can be a string or any Microsoft Office Excel data type.

After
Type: System.Object

The cell after which you want the search to begin. This corresponds to the position of the active cell when a search is done from the user interface. Note that After must be a single cell in the range. Remember that the search begins after this cell; the specified cell is not searched until the method wraps back around to this cell. If you do not specify this argument, the search starts after the cell in the upper-left corner of the range.

LookIn
Type: System.Object

The type of information.

LookAt
Type: System.Object

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

SearchOrder
Type: System.Object

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

SearchDirection
Type: Microsoft.Office.Interop.Excel.XlSearchDirection

The search direction.

Can be one of the following XlSearchDirection values:

xlNext or xlPrevious.

MatchCase
Type: System.Object

true to make the search case-sensitive. The default value is false.

MatchByte
Type: System.Object

Used only if you have selected or installed double-byte language support. true to have double-byte characters match only double-byte characters; false to have double-byte characters match their single-byte equivalents.

SearchFormat
Type: System.Object

The search format.

Return Value

Type: Microsoft.Office.Interop.Excel.Range
A Microsoft.Office.Interop.Excel.Range object that represents the first cell where the information is found.

This method returns null if no match is found.

This method does not affect the selection or the active cell.

The settings for LookIn, LookAt, SearchOrder, 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.

You can use the FindNext and FindPrevious methods to repeat the search.

When the search reaches the end of the specified search range, it wraps around to the beginning of the range. To stop a search when this wraparound occurs, save the address of the first found cell, and then test each successive found-cell address against this saved address.

Optional Parameters

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

The following code example uses the Find method to find the first cell with the value Seashell in a NamedRange control. The example then uses the FindNext and FindPrevious methods to find the next cell with the value Seashell and then return to the original cell. Finally, the example uses the Cut method to cut the contents of the first cell with the value Seashell and paste it into cell B1.

This example is for a document-level customization.

private void FindValue()
{
    this.Range["A1"].Value2 = "Barnacle";
    this.Range["A2"].Value2 = "Seashell";
    this.Range["A3"].Value2 = "Star Fish";
    this.Range["A4"].Value2 = "Seashell";
    this.Range["A5"].Value2 = "Clam Shell";

    Microsoft.Office.Tools.Excel.NamedRange namedRange1 =
        this.Controls.AddNamedRange(this.Range["A1", "A5"],
        "namedRange1");

    // Find the first occurrence of "Seashell".
    Excel.Range Range1 = namedRange1.Find("Seashell",
        Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByColumns,
        Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext,
        false);

    // Find the next occurrence of "Seashell".
    Range1 = namedRange1.FindNext(Range1);

    // Return to the first occurrence of "Seashell".
    Range1 = namedRange1.FindPrevious(Range1);

    // Cut the range with the first "Seashell" and copy it to cell B1.
    Microsoft.Office.Tools.Excel.NamedRange namedRange2 =
        this.Controls.AddNamedRange(Range1, "namedRange2");
    namedRange2.Cut(this.Range["B1"]);
}

Show:
© 2014 Microsoft