Article ID: 123578
Article Last Modified on 8/15/2005
' 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
' 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
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()
Additional query words: 4.00a 5.00c 8.00 continuous cumulative XL
Keywords: kbhowto kbprogramming kbdtacode KB123578