OnEntry 매크로를 사용하여 Excel의 셀 주석에 실행 중인 합계 만들기

요약

Microsoft Excel에서는 워크시트의 계산이 아닌 부분에 결과를 저장하여 실행 중인 합계를 만들 때 순환 참조를 방지할 수 있습니다. 이 문서에는 실행 중인 합계를 셀 주석에 저장하여 이 작업을 수행하는 샘플 Microsoft Visual Basic for Applications 프로시저가 포함되어 있습니다.

추가 정보

Microsoft에서 제공하는 프로그래밍 예제는 예시를 위한 것일 뿐이며 이와 관련하여 명시적이거나 묵시적인 어떠한 보증도 하지 않습니다. 이는 상품성이나 특정 목적에 대한 적합성의 묵시적인 보증을 포함하며 이에 제한되지 않습니다. 이 문서에서는 예제에 사용되고 있는 프로그래밍 언어와 프로시저를 만들고 디버깅하는 데 사용되는 도구를 사용자가 잘 알고 있는 것으로 가정합니다. Microsoft 지원 엔지니어는 사용자에게 도움이 되도록 특정 프로시저에 대한 기능을 설명할 수 있지만 사용자의 특정 요구 사항에 맞도록 예제를 수정하여 추가 기능을 제공하거나 프로시저를 구성하지는 않습니다.

셀에서 실행 합계를 만들려면

  1. Microsoft Excel에서 새 통합 문서를 엽니다.

  2. Visual Basic 편집기 시작합니다(Alt+F11 누르기).

  3. 삽입 메뉴에서 모듈을 클릭합니다.

  4. 이 모듈에 다음 매크로를 입력합니다.

       ' The Auto_Open name forces this macro to run every time
       ' the workbook containing this macro is opened.
    
    Sub Auto_Open()
       '  Every time a cell's value is changed,
       '  the RunningTotal macro runs.
          Application.OnEntry = "RunningTotal"
       End Sub
    
       '----------------------------------------------------------
       ' This macro runs each time the value of a cell changes.
       ' It adds the current value of the cell to the value of the
       ' cell comment. Then it stores the new total in the cell comment.
       Sub RunningTotal()
    
    On Error GoTo errorhandler      ' Skip cells that have no comment.
    
    With Application.Caller
    
       '     Checks to see if the cell is a running total by
       '     checking to see if the first 4 characters of the cell
       '     comment are "RT= ". NOTE: there is a space after the equal
       '     sign.
             If Left(.Comment.Text, 4) = "RT= " Then
    
       '        Change the cell's value to the new value in the cell
       '        plus the old total stored in the cell comment.
                RT = .Value + Right(.Comment.Text, Len(.Comment.Text) - 4)
                .Value = RT
    
       '        Store the new total in the cell note.
                .Comment.Text Text:="RT= " & RT
            End If
          End With
    
    Exit Sub      ' Skip over the errorhandler routine.
    
    errorhandler: ' End the procedure if no comment in the cell.
          Exit Sub
    
    End Sub
    
       '--------------------------------------------------------------
       ' This macro sets up a cell to be a running total cell.
       Sub SetComment()
          With ActiveCell
       '     Set comment to indicate that a running total is present.
       '     If the ActiveCell is empty, multiplying by 1 will
       '     return a 0.
             .AddComment
             .Comment.Text Text:="RT= " & (ActiveCell * 1)
          End With
       End Sub
    
  5. 매크로를 입력한 후 파일 메뉴에서 닫기 및 Microsoft Excel로 돌아가기를 클릭합니다.

  6. 통합 문서를 저장하고 닫은 다음 다시 엽니다.

    입력한 Auto_Open 매크로는 통합 문서를 열 때 실행됩니다.

  7. C3 셀을 선택합니다.

    실행 중인 합계가 있는 주석이 포함된 셀입니다.

  8. SetComment 매크로를 실행하려면 다음 단계를 수행합니다.

    1. [도구] 메뉴에서 [매크로]를 가리킨 다음 [매크로]를 클릭합니다.
    2. 매크로 대화 상자에서 SetComment를 클릭한 다음 실행을 클릭합니다.

실행 합계 사용 예제

실행 중인 합계를 사용하려면 다음 단계를 수행합니다.

  1. C3 셀에 숫자 10을 입력합니다.
  2. 셀 C3을 선택하고 주석이 "RT= 10"(따옴표 없이)을 표시합니다.
  3. C3 셀에 숫자 7을 입력합니다.
  4. 셀 C3을 선택하고 주석이 "RT= 17"(따옴표 없이)을 표시합니다.

실행 중인 합계를 제거하려면

실행 중인 합계를 제거하려면 다음 단계를 수행합니다.

  1. 제거할 실행 합계가 포함된 셀을 선택합니다.
  2. 셀을 마우스 오른쪽 단추로 클릭하고 바로 가기 메뉴에서 메모 삭제를 클릭합니다.