다음을 통해 공유


방법: 프로그래밍 방식으로 워크시트 범위에서 텍스트 검색

Microsoft.Office.Interop.Excel.Range 개체의 Find 메서드를 사용하면 범위 내에서 텍스트를 검색할 수 있습니다.이 텍스트는 #NULL! 또는 #VALUE!와 같이 워크시트 셀에 나타날 수 있는 오류 문자열일 수도 있습니다.오류 문자열에 대한 자세한 내용은 Cell Error Values를 참조하십시오.

적용 대상: 이 항목의 정보는 Excel 2013 및 Excel 2010의 문서 수준 프로젝트 및 응용 프로그램 수준 프로젝트에 적용됩니다. 자세한 내용은 Office 응용 프로그램 및 프로젝트 형식에 따라 사용 가능한 기능을 참조하십시오.

다음 예제에서는 Fruits라는 범위를 검색하고 "apples"라는 단어가 포함된 셀의 글꼴을 수정합니다.이 프로시저에서는 이전에 지정한 검색 설정을 사용하여 검색을 반복하는 FindNext 메서드도 사용합니다.검색을 시작할 셀만 지정하면 나머지는 FindNext 메서드에 의해 자동으로 처리됩니다.

[!참고]

FindNext 메서드를 사용할 때 검색 범위의 끝에 도달하면 검색 범위의 시작 부분으로 돌아와 검색 작업이 이어집니다.따라서 검색 작업이 무한 루프에 빠지지 않도록 코드를 작성해야 합니다.다음 샘플 프로시저에서는 이 문제를 처리하기 위해 Address 속성을 사용하는 한 가지 방법을 보여 줍니다.

비디오에 링크 관련 비디오 데모를 보려면 How Do I: Use the Find Method in an Excel Add-in?을 참조하십시오.

워크시트 범위에서 텍스트를 검색하려면

  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(Excel.XlReferenceStyle.xlA1)
          == firstFind.get_Address(Excel.XlReferenceStyle.xlA1))
    {
        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(Excel.XlReferenceStyle.xlA1)
              == firstFind.get_Address(Excel.XlReferenceStyle.xlA1))
        {
            break;
        }

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

        currentFind = Fruits.FindNext(currentFind); 
    }
}

참고 항목

작업

방법: 프로그래밍 방식으로 통합 문서에서 일정 범위에 스타일 적용

방법: 프로그래밍 방식으로 코드에서 워크시트 범위 참조

개념

범위 작업

Office 솔루션의 선택적 매개 변수