次の方法で共有


方法 : ワークシートの範囲内のテキストを検索する

更新 : 2007 年 11 月

対象

このトピックの情報は、指定された Visual Studio Tools for Office プロジェクトおよび Microsoft Office のバージョンにのみ適用されます。

プロジェクトの種類

  • ドキュメント レベルのプロジェクト

  • アプリケーション レベルのプロジェクト

Microsoft Office のバージョン

  • Excel 2003

  • Excel 2007

詳細については、「アプリケーションおよびプロジェクトの種類別の使用可能な機能」を参照してください。

Microsoft.Office.Interop.Excel.Range オブジェクトの Find メソッドを使用すると、範囲内のテキストを検索できます。このテキストは、ワークシートのセルに表示できるエラー文字列にすることもできます (#NULL! や #VALUE!)。エラー文字列の詳細については、「Cell Error Values」を参照してください。

以下の例では、"Fruits" という名前の範囲を検索し、単語 "apples" が含まれているセルのフォントを変更します。このプロシージャでは、FindNext メソッドも使用して、以前に設定された検索設定を利用して検索を繰り返します。検索対象の後にあるセルを指定して、FindNext メソッドで残りのセルを処理します。

e4x1k99a.alert_note(ja-jp,VS.90).gifメモ :

FindNext メソッドによる検索は、検索処理が検索範囲の末尾に達すると、検索範囲の先頭に戻ります。コードでは、検索処理が無限ループにならないようにする必要があります。サンプル プロシージャでは、その方法の 1 つとして、Address プロパティを使用する方法を示します。

ワークシートの範囲内のテキストを検索するには

  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 ソリューションの省略可能なパラメータについて