如何:在工作表范围内搜索文本

通过 Microsoft.Office.Interop.Excel.Range 对象的 Find 方法,可以在范围中搜索文本。 此文本还可以是工作表单元格中显示的任何错误字符串,例如 #NULL! 或 #VALUE!。 有关错误字符串的更多信息,请参见单元格错误值

**适用于:**本主题中的信息适用于 Excel 2007 和 Excel 2010 的文档级项目和应用程序级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能

下面的示例在名为 Fruits 的范围内进行搜索,并修改包含单词“apples”的单元格的字体。 此过程还使用 FindNext 方法,该方法使用前面设定的搜索设置重复搜索。 您需要指定要从哪个单元格后开始搜索,FindNext 方法将处理其余操作。

提示

FindNext 方法在到达搜索范围末尾后将重新回到范围开始处进行搜索。 您的代码必须确保搜索不会进入死循环。 该示例过程演示了一种使用 Address 属性处理这种情况的方法。

链接到视频 有关相关视频演示,请参见 How Do I: Use the Find Method in an Excel Add-in?(如何实现:在 Excel 外接程序中使用查找方法?)。

在工作表范围内搜索文本

  1. 声明若干变量,分别用于跟踪整个范围、找到的第一个范围和当前找到的范围。

    Dim currentFind As Excel.Range = Nothing
    Dim firstFind As Excel.Range = Nothing
    
    Excel.Range currentFind = null; 
    Excel.Range firstFind = null; 
    
  2. 搜索第一个匹配项,指定从其后开始搜索的单元格以外的所有参数。

    currentFind = Fruits.Find("apples", , _
        Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, _
        Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, False)
    
    currentFind = Fruits.Find("apples", missing,
        Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, 
        Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
        missing, missing); 
    
  3. 继续搜索,直到没有匹配项。

    While Not currentFind Is Nothing
    
    while(currentFind != null) 
    
  4. 将找到的第一个范围 (firstFind) 和 Nothing 进行比较。 如果 firstFind 不包含任何值,则代码将存储查找范围 (currentFind)。

    If firstFind Is Nothing Then
        firstFind = currentFind
    
    if (firstFind == null)
    {
        firstFind = currentFind; 
    }
    
  5. 如果查找范围的地址与第一个查找范围的地址匹配,则退出循环。

    ElseIf currentFind.Address = firstFind.Address Then
        Exit While
    End If
    
    else if (currentFind.get_Address(missing, missing, Excel.XlReferenceStyle.xlA1, missing, missing)
          == firstFind.get_Address(missing, missing, Excel.XlReferenceStyle.xlA1, missing, missing))
    {
        break;
    }
    
  6. 设置找到的范围的外观。

    With currentFind.Font
        .Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
        .Bold = True
    End With
    
    currentFind.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    currentFind.Font.Bold = true; 
    
  7. 再次执行搜索。

    currentFind = Fruits.FindNext(currentFind)
    
    currentFind = Fruits.FindNext(currentFind); 
    

下面的示例演示完整的方法。

示例

Private Sub DemoFind()
    Dim currentFind As Excel.Range = Nothing
    Dim firstFind As Excel.Range = Nothing

    Dim Fruits As Excel.Range = Me.Application.Range("A1", "B2")
    ' You should specify all these parameters every time you call this method, 
    ' since they can be overridden in the user interface.
    currentFind = Fruits.Find("apples", , _
        Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, _
        Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, False)

    While Not currentFind Is Nothing

        ' Keep track of the first range you find.
        If firstFind Is Nothing Then
            firstFind = currentFind

        ' If you didn't move to a new range, you are done.
        ElseIf currentFind.Address = firstFind.Address Then
            Exit While
        End If

        With currentFind.Font
            .Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
            .Bold = True
        End With

        currentFind = Fruits.FindNext(currentFind)
    End While
End Sub
private void DemoFind() 
{
    Excel.Range currentFind = null; 
    Excel.Range firstFind = null; 

    Excel.Range Fruits = Application.get_Range("A1", "B3");
    // You should specify all these parameters every time you call this method,
    // since they can be overridden in the user interface. 
    currentFind = Fruits.Find("apples", missing,
        Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, 
        Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
        missing, missing); 

    while(currentFind != null) 
    { 
        // Keep track of the first range you find. 
        if (firstFind == null)
        {
            firstFind = currentFind; 
        }

        // If you didn't move to a new range, you are done.
        else if (currentFind.get_Address(missing, missing, Excel.XlReferenceStyle.xlA1, missing, missing)
              == firstFind.get_Address(missing, missing, Excel.XlReferenceStyle.xlA1, missing, missing))
        {
            break;
        }

        currentFind.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
        currentFind.Font.Bold = true; 

        currentFind = Fruits.FindNext(currentFind); 
    }
}

请参见

任务

如何:将样式应用于工作簿中的范围

如何:使用代码引用工作表范围

概念

使用范围

Office 解决方案中的可选参数