Share via


XmlMappedRange.Find Method

Finds specific information in an XmlMappedRange control, and returns a Range 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)

Syntax

'Declaration
Function Find ( _
    What As Object, _
    After As Object, _
    LookIn As Object, _
    LookAt As Object, _
    SearchOrder As Object, _
    SearchDirection As XlSearchDirection, _
    MatchCase As Object, _
    MatchByte As Object, _
    SearchFormat As Object _
) As Range
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.

  • 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.

Return Value

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

Remarks

This method returns nulla null reference (Nothing in Visual Basic) 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.

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 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
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);
}

.NET Framework Security

See Also

Reference

XmlMappedRange Interface

Microsoft.Office.Tools.Excel Namespace