Partager via


NamedRange.FindPrevious Method

Continues a search that was begun with the Find method.

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

Syntax

'Declaration
Function FindPrevious ( _
    After As Object _
) As Range
Range FindPrevious(
    Object After
)

Parameters

  • After
    Type: System.Object

    The cell before which you want to search. 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 NamedRange control. Remember that the search begins before this cell; the specified cell is not searched until the method wraps back around to this cell. If this argument is not specified, the search starts before the upper-left cell in the NamedRange control.

Return Value

Type: Microsoft.Office.Interop.Excel.Range
A Microsoft.Office.Interop.Excel.Range object that represents a cell containing the specified information.

Remarks

Finds the previous cell that matches the same conditions as the Find method.

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

When the search reaches the beginning of the specified search range, it wraps around to the end 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.

Examples

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 Sub FindValue()
        Me.Range("A1").Value2 = "Barnacle" 
        Me.Range("A2").Value2 = "Seashell" 
        Me.Range("A3").Value2 = "Star Fish" 
        Me.Range("A4").Value2 = "Seashell" 
        Me.Range("A5").Value2 = "Clam Shell" 

        Dim namedRange1 As Microsoft.Office.Tools.Excel.NamedRange _
            = Me.Controls.AddNamedRange(Me.Range("A1", "A5"), _
            "namedRange1")

        ' Find the first occurrence of "Seashell". 
        Dim Range1 As Excel.Range = namedRange1.Find("Seashell", , , _
            Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByColumns, _
            Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, _
            False, 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. 
        Dim namedRange2 As Microsoft.Office.Tools.Excel.NamedRange _
            = Me.Controls.AddNamedRange(Range1, "namedRange2")
        namedRange2.Cut(Me.Range("B1"))
    End Sub
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"]);
}

.NET Framework Security

See Also

Reference

NamedRange Interface

Microsoft.Office.Tools.Excel Namespace