XL: OnEntry Macro to Create a Running Total
ID: Q123578
|
The information in this article applies to:
-
Microsoft Excel 97 for Windows
-
Microsoft Excel for Windows 95, version 7.0
-
Microsoft Excel for Windows, versions 4.0, 4.0a, 5.0, 5.0c
-
Microsoft Excel 98 Macintosh Edition
-
Microsoft Excel for the Macintosh, versions 4.0, 5.0, 5.0a
SUMMARY
In Microsoft Excel, to avoid circular references when you create a running
total in a cell, the total must be stored in a non-calculating part of the
worksheet. This result can be achieved through a Microsoft Visual Basic
for Applications macro (Sub procedure). The sample macros in the "More
Information" section of this article use cell notes or comments (in Excel
97 and Excel 98) to store totals.
MORE INFORMATION
Microsoft provides programming examples for illustration only, without
warranty either expressed or implied, including, but not limited to, the
implied warranties of merchantability and/or fitness for a particular
purpose. This article assumes that you are familiar with the programming
language being demonstrated and the tools used to create and debug
procedures. Microsoft support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to
provide added functionality or construct procedures to meet your specific
needs. If you have limited programming experience, you may want to contact
the Microsoft fee-based consulting line at (800) 936-5200. For more
information about the support options available from Microsoft, please see
the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp To Create a Running Total in a Cell
- Enter the macro that applies to your version of Microsoft Excel.
Excel 97 and Excel 98
- Start the Visual Basic Editor (press ALT+F11).
- On the Insert menu click Module.
This will insert a module into your project.
- Type the macros following step d into this module.
- After typing the macros, click "Close and Return to Microsoft Excel"
on the File menu.
' 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
Excel versions 5.0 and 7.0
- On the Insert menu, point to Macro, and then click Module.
This will insert a new module sheet into your workbook.
- Type the following macros into this module sheet:
' 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 sets up a cell to be a running total cell.
Sub SetNote()
With ActiveCell
' Set note to indicate that a running total is present.
' If the ActiveCell is empty, multiplying by 1 will
' return a 0.
.NoteText Text:="RT_" + (ActiveCell * 1)
End With
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 note. Then it stores the new total in the cell note.
Sub RunningTotal()
With Application.Caller
' Checks to see if the cell is a running total by
' checking to see if the first 3 characters of the cell
' note are "RT_".
If .NoteText(length:=3) = "RT_" Then
' Change the cell's value to the new value in the cell
' plus the old total stored in the cell note.
.Value = .Value + .NoteText(start:=4)
' Store the new total in the cell note.
.NoteText "RT_" + .Value
End If
End With
End Sub
Excel 4.0 Macro Code Example
After you enter the macros, define Auto_Open, SetNote, and
RunningTotal as command macros that refer to A1, A5, and A9
respectively.
A1: Auto_Open
A2: =ON.ENTRY(,"RunningTotal")
A3: =RETURN()
A4:
A5: SetNote
A6: =NOTE("RT_"&ACTIVE.CELL()*1)
A7: =RETURN()
A8:
A9: RunningTotal
A10: =IF(GET.NOTE(,,3)="RT_")
A11: = FORMULA(CALLER()+GET.NOTE(,4),CALLER())
A12: = NOTE("RT_"&CALLER())
A13: =END.IF()
A14: =RETURN()
Explanation of macro:
- Cell A1 defines this cell as Auto_Open which causes this macro to
run every time the macro sheet is opened.
- Cell A2 runs the RunningTotal macro every time a cell's value is
changed.
- Cell A3 ends the Auto_Open macro.
- Cell A5 contains the name of the macro that sets up a cell to be
a running total cell.
- Cell A6 sets the active cell's note to be "RT_" & the value of
the active cell.
- Cell A7 ends the SetNote macro.
- Cell A9 contains the macro that runs each time the value of a
cell changes. It adds the current value of the cell to the value
of the cell note. Then it stores the new total in the cell note.
- Cell A10 checks to see if the cell is a running total by checking
to see if the first 3 characters of the cell note are "RT_".
- Cell A11 changes the cell's value to the new value in the cell
and adds the old total stored in the cell note.
- Cell A12 stores the new total in the cell note.
- Cell A13 ends the IF() statement.
- Cell A14 ends the RunningTotal macro.
- Save and close your file, and then re-open it.
The Auto_Open macro you typed will be executed when you open your file.
- Select the cell that you want to contain the running total.
- Follow the appropriate procedure below to run the SetNote (SetComment
macro for Microsoft Excel 97) macro.
Microsoft Excel 97
- On the Tools menu point to Macro and then click Macros.
- In the Macro dialog box click SetComment and then click Run.
Microsoft Excel version 5.0 or 7.0
- On the Tools menu, click Macro.
- From the Macro Name/Reference list, select the macro SetNote,
and then click Run.
Microsoft Excel version 4.0
- On the Macro menu, click Run.
- Select the SetNote macro, and then click OK.
To Remove a Running Total
To remove a running total, follow the appropriate procedure below for
your version of Microsoft Excel.
Excel 97 and Excel 98
- Select the cell that contains the running total you want removed.
- Right-click the cell and click "Delete Comment" on the shortcut
menu.
Excel versions 5.0 and 7.0
- Select the cell on the worksheet that contains the running total.
- On the Insert menu, click Note, and then click Delete.
Excel version 4.0
- Select the cell on the worksheet that contains the running total.
- On the Formula menu, click Note, and then click Delete.
Additional query words:
4.00a 5.00c 8.00 continuous cumulative
Keywords : kbprg kbdta kbdtacode PgmOthr PgmHowto xlformula KbVBA
Version : MACINTOSH:4.0,5.0,5.0a,98; WINDOWS:4.0,4.0a,5.0,5.0c,97; Win95:7.0
Platform : MACINTOSH Win95 WINDOWS
Issue type : kbhowto
|