NamedRange.FindPrevious 方法

继续执行用 Find 方法开始的搜索。

命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel(在 Microsoft.Office.Tools.Excel.dll 中)

语法

声明
Function FindPrevious ( _
    After As Object _
) As Range
Range FindPrevious(
    Object After
)

参数

  • After
    类型:System.Object
    要在其之前搜索的单元格。此单元格就是从用户界面执行的搜索完成后处于活动状态的单元格。注意,After 必须是 NamedRange 控件中的单个单元格。请记住,搜索将从该单元格之后开始;只有方法绕回到指定的单元格之后,才会搜索该单元格。如果未指定此参数,则从 NamedRange 控件左上角单元格之前开始搜索。

返回值

类型:Microsoft.Office.Interop.Excel.Range
一个 Microsoft.Office.Interop.Excel.Range 对象,表示包含指定信息的单元格。

备注

查找上一个与 Find 方法匹配相同条件的单元格。

此方法不会影响选定内容或活动单元格。

当搜索功能到达指定搜索范围的开始时,它会绕回到该范围的末尾。若要在发生此绕回操作时停止搜索,请保存找到的第一个单元格的地址,然后针对已保存的这个地址测试以后找到的每个单元格地址。

可选参数

有关可选参数的信息,请参见Office 解决方案中的可选参数

示例

下面的代码示例使用 Find 方法,在 NamedRange 控件中查找值为 Seashell 的第一个单元格。该示例然后使用 FindNext 和 FindPrevious 方法查找值为 Seashell 的下一个单元格,然后返回到原始单元格。最后,该示例使用 Cut 方法剪切值为 Seashell 的第一个单元格的内容并将其粘贴到单元格 B1 中。

此示例针对的是文档级自定义项。

    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 安全性

请参见

参考

NamedRange 接口

Microsoft.Office.Tools.Excel 命名空间