Accumulator

Situation: 

You want to summarize some aspect of a data set, like the average, or highest value.

Actions: 

Use a variable as an accumulator.

  • Initialize the variable.
  • Loop over the cells you want to process. Update the value of the accumulator each time through the loop.
  • After the loop, the accumulator has the value you want.
Explanation: 

Your program processes one cell at a time, in a loop. The accumulator accumulates the loop’s processing.

total is an accumulator in this code:

  1. Private Sub cmdRun_Click()
  2.     Dim total As Single
  3.     Dim row As Integer
  4.     total = 0
  5.     For row = 1 To 5
  6.         total = total + Cells(row, 1)
  7.     Next row
  8.     Cells(8, 2) = total
  9. End Sub