使用 OnEntry 宏在 Excel 的儲存格批注中建立執行中的總計

摘要

在 Microsoft Excel 中,您可以藉由將結果儲存在工作表的非計算部分來建立執行中總計,以避免循環參考。 本文包含範例 Microsoft Visual Basic for Applications 程式,可藉由將執行中的總計儲存在單元格批注中來執行此動作。

其他相關資訊

Microsoft 提供的程式設計範例僅供說明之用,並不具任何明示或暗示的責任擔保。 這包括 (但不限於) 任何目的之適售性及適用性的暗示責任擔保。 本文假設您熟悉示範的程式設計語言,也熟悉用以建立和偵錯程序的工具。 Microsoft 技術支援工程師可以協助說明特定程序的功能,但不會修改這些範例以提供附加功能或建構程序來滿足您的特定需求。

在儲存格中建立執行中總計

  1. 在 Microsoft Excel 中開啟新的活頁簿。

  2. 啟動 Visual Basic 編輯器 (按 ALT+F11) 。

  3. 在 [插入] 功能表上,按一下 [模組]

  4. 在此課程模組中輸入下列宏:

    VB
       ' 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. 以滑鼠右鍵單擊單元格,然後按下快捷方式功能表上的 [刪除批註]。