Share via


XmlMappedRange.Find Method

Definition

Finds specific information in an XmlMappedRange control, and returns a Range that represents the first cell where that information is found.

public Microsoft.Office.Interop.Excel.Range Find (object What, object After, object LookIn, object LookAt, object SearchOrder, Microsoft.Office.Interop.Excel.XlSearchDirection SearchDirection = Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, object MatchCase, object MatchByte, object SearchFormat);
abstract member Find : obj * obj * obj * obj * obj * Microsoft.Office.Interop.Excel.XlSearchDirection * obj * obj * obj -> Microsoft.Office.Interop.Excel.Range
Public Function Find (What As Object, Optional After As Object, Optional LookIn As Object, Optional LookAt As Object, Optional SearchOrder As Object, Optional SearchDirection As XlSearchDirection = Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, Optional MatchCase As Object, Optional MatchByte As Object, Optional SearchFormat As Object) As Range

Parameters

What
Object

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

After
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
Object

The type of information.

LookAt
Object

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

SearchOrder
Object

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

SearchDirection
XlSearchDirection

The search direction.Can be one of the following XlSearchDirection values: xlNext or xlPrevious.

MatchCase
Object

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

MatchByte
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
Object

The search format.

Returns

A Range that represents the first cell where the specified information is found.

Examples

The following code example sets the value of an XmlMappedRange to the string "Smith", and then uses the Find, FindNext, and FindPrevious methods to find the first cell with the string "Smith". Because an XmlMappedRange always contains exactly one cell, the same cell is found in each case. This code example assumes that the current worksheet contains an XmlMappedRange named CustomerLastNameCell.

private void FindSmith()
{
    this.CustomerLastNameCell.Value2 = "Smith";

    // Use Find to get the range with "Smith".
    Excel.Range range1 = this.CustomerLastNameCell.Find("Smith",
        Excel.XlSearchDirection.xlNext);
    string address1 = range1.get_Address(missing, missing,
        Excel.XlReferenceStyle.xlA1);
    MessageBox.Show("Find method found the range: " + address1);

    // Use FindNext to get the range with "Smith".
    Excel.Range range2 = this.CustomerLastNameCell.FindNext(range1);
    string address2 = range2.get_Address(
        Excel.XlReferenceStyle.xlA1);
    MessageBox.Show("FindNext method found the range: " + address2);

    // Use FindPrevious to get the range with "Smith".
    Excel.Range range3 = this.CustomerLastNameCell.FindPrevious(range2);
    string address3 = range3.get_Address(
        Excel.XlReferenceStyle.xlA1);
    MessageBox.Show("FindPrevious method found the range: " + address3);
}
Private Sub FindSmith()
    Me.CustomerLastNameCell.Value2 = "Smith"

    ' Use Find to get the range with "Smith".
    Dim range1 As Excel.Range = Me.CustomerLastNameCell.Find( _
        "Smith", SearchDirection:=Excel.XlSearchDirection.xlNext)
    Dim address1 As String = range1.Address(ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("Find method found the range: " & address1)

    ' Use FindNext to get the range with "Smith".
    Dim range2 As Excel.Range = Me.CustomerLastNameCell.FindNext(range1)
    Dim address2 As String = range2.Address(ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("FindNext method found the range: " & address2)

    ' Use FindPrevious to get the range with "Smith".
    Dim range3 As Excel.Range = Me.CustomerLastNameCell.FindPrevious(range2)
    Dim address3 As String = range3.Address(ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("FindPrevious method found the range: " & address3)
End Sub

Remarks

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 Microsoft.Office.Interop.Excel.Range.FindNext* and FindPrevious methods to repeat the search.

Applies to