共用方式為


HOW TO:在工作表範圍中搜尋文字

Microsoft.Office.Interop.Excel.Range 物件的 Find 可讓您在範圍內搜尋文字。 這個文字也可以是可能出現在工作表儲存格中的任何錯誤字串,例如 #NULL! 或 #VALUE!。 如需錯誤字串的詳細資訊,請參閱儲存格錯誤值

**適用於:**本主題中的資訊適用於 Excel 2007 和 Excel 2010 的文件層級專案和應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

下列範例會搜尋名為 Fruits 的範圍,並修改含有 "apples" 這個字的儲存格字型。 這項程序還會使用 FindNext 方法,這個方法使用之前設定的搜尋設定來重複搜尋。 您必須指定要搜尋的下一個儲存格,然後 FindNext 方法就會處理其餘的部分。

注意事項注意事項

FindNext 方法的搜尋到達範圍的結尾之後,會再繞回到搜尋範圍的起點。 您必須確定程式碼搜尋不會在無窮迴圈中反覆環繞。 範例程序會示範使用 Address 屬性處理這種情況的方式。

視訊的連結 如需觀看相關示範影片,請參閱如何在 Excel 增益集中使用 Find 方法?(英文)。

若要在工作表範圍中搜尋文字

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

請參閱

工作

HOW TO:將樣式套用至活頁簿中的範圍

HOW TO:在程式碼中參考工作表範圍

概念

使用範圍

Office 方案中的選擇性參數