更新:2007 年 11 月
| 适用于 | 
|---|
| 本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。 项目类型 
 Microsoft Office 版本 
 有关更多信息,请参见按应用程序和项目类型提供的功能。 | 
使用 Microsoft.Office.Interop.Excel.Range 对象的 Find 方法可以在范围中搜索文本。此文本还可以是工作表单元格中显示的任何错误字符串,例如 #NULL! 或 #VALUE!。有关错误字符串的更多信息,请参见单元格错误值。
下面的示例在名为 Fruits 的范围内进行搜索,并修改包含单词“apples”的单元格的字体。此过程还使用 FindNext 方法,该方法使用前面设定的搜索设置重复搜索。您需要指定要从哪个单元格后开始搜索,FindNext 方法将处理其余操作。
| .gif) 说明: | 
|---|
| FindNext 方法在到达搜索范围末尾后将重新回到范围开始处进行搜索。您的代码必须确保搜索不会进入死循环。该示例过程演示了一种使用 Address 属性处理这种情况的方法。 | 
在工作表范围内搜索文本
- 声明若干变量,分别用于跟踪整个范围、找到的第一个范围和当前找到的范围。 - Dim currentFind As Excel.Range = Nothing Dim firstFind As Excel.Range = Nothing- Excel.Range currentFind = null; Excel.Range firstFind = null;
- 搜索第一个匹配项,指定从其后开始搜索的单元格以外的所有参数。 - 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);
- 继续搜索,直到没有匹配项。 - While Not currentFind Is Nothing- while(currentFind != null)
- 将找到的第一个范围 (firstFind) 和 Nothing 进行比较。如果 firstFind 不包含任何值,则代码将存储查找范围 (currentFind)。 - If firstFind Is Nothing Then firstFind = currentFind- if (firstFind == null) { firstFind = currentFind; }
- 如果查找范围的地址与第一个查找范围的地址匹配,则退出循环。 - 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; }
- 设置找到的范围的外观。 - 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;
- 再次执行搜索。 - 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); 
    }
}