Search

June 29, 2026

Using variables inside a ForEach to calculate totals

Keeping a running total and displaying the final total after a ForEach loop

When working with repeating data inside a ForEach(), you may need to keep a running total and display the final result after the loop has finished. An example is calculating the total value, quantity, or square footage from a list of items.

 

Step 1: Set the variable

Before the ForEach() begins, you need to set the variable with a starting value:

«SetVr(‘TotalProjectFt’,0)»

This creates a variable named TotalProjectFt and sets its value to 0.

 

 

Step 2: Add to the variable inside the ForEach

Inside the ForEach(), use IncrementVr to increment the variable value during each iteration:

«IncrementVr(‘TotalProjectFt’,RenderAsNumeric(BuildingSquareFootage*QuantityOfBuildings))»

Each time the loop runs, the calculated value is added to the running total. In this example, TotalProjectFt is increased by the result of BuildingSquareFootage x QuantityOfBuildings for each record in the grid.

 

 

Step 3: Display the total after the ForEach

After the End(ForEach), retrieve the total value using GetV:

«FormatNumber(GetV(‘TotalProjectFt’),’0;’)?»

This displays the final accumulated total.

 

 

Important Notes:

  • Set the variable before the ForEach() starts.
  • Use IncrementV or IncrementVr (if you want the paragraph to be removed) to add values during each iteration.
  • Use GetV after the ForEach() has finished to retrieve the final value.
  • Variable names must be enclosed in quotes: ‘TotalProjectFt’ or “TotalProjectFt”.
  • If the data element does not have a numeric value, use GetVn to ensure the values are treated as numbers.

 

Further reading: