“Every fool can know. The point is to understand.” [Albert Einstein]

Abstract

Note: A detailed PDF document including usage examples you can find in the Download section below.

How can you ensure that the sum of rounded percentages equals exactly 100%? Is it possible to make sure that, for accounting purposes, the distribution of overhead costs precisely matches the original total? These issues are well-known and have been studied for a long time. This article presents a simple solution using Excel / VBA. The function introduced can round relative values (percentages) to exactly 100%, and it can round absolute values (such as cost distributions) while preserving the original rounded sum. A parameter allows you to choose which type of error to minimize — absolute error or relative error — compared to common half-up rounding.

The Excel / VBA Function RoundToSum

Name

RoundToSum – Rounding values preserving their rounded sum

Synopsis

RoundToSum(vInput, [lDigits], [bAbsSum], [lErrorType], [bDontAmend])

Description

RoundToSum rounds values without altering their rounded sum. It uses the largest remainder method to minimize the error compared to the commonly used half-up rounding method. If the error is identical for one or more values, the first value(s) encountered will be adjusted.

Note: This solution is limited to one-dimensional tables without subtotals. There is no general solution for higher-dimensional tables or tables with subtotals.

Parameters

vInput – Range or array containing the unrounded input values.

lDigits – Optional, default value is 2. The number of digits to round to. For example: 0 rounds to integers, 2 rounds to the nearest cent, -3 rounds to the nearest thousand.

bAbsSum – Optional, default value is TRUE. TRUE rounds the values directly. FALSE adjusts the percentages so they sum to exactly 100%.

lErrorType – Optional, default value is 1. The type of error to minimize: 1 for absolute error, 2 for relative error.

bDontAmend – Optional, default value is FALSE. TRUE prevents adjusting the values to match the rounded sum. FALSE makes adjustments as described above. This parameter is mainly for demonstration purposes to see the function’s impact.

Appendix – RoundToSum Code

Please read my Disclaimer.

Option Explicit

Enum mc_Macro_Categories
  mcFinancial = 1
  mcDate_and_Time
  mcMath_and_Trig
  mcStatistical
  mcLookup_and_Reference
  mcDatabase
  mcText
  mcLogical
  mcInformation
  mcCommands
  mcCustomizing
  mcMacro_Control
  mcDDE_External
  mcUser_Defined
  mcFirst_custom_category
  mcSecond_custom_category 'and so on
End Enum 'mc_Macro_Categories

Function RoundToSum(vInput As Variant, Optional lDigits As Long = 2, Optional bAbsSum As Boolean = True, _
    Optional lErrorType As Long = 1, Optional bDontAmend As Boolean = False) As Variant
'Calculate rounded summands which exactly add up to the rounded sum of unrounded summands.
'It uses the largest remainder method which minimizes the error to the original unrounded summands.
'V2.1 PB 12-Oct-2024 (C) (P) by Bernd Plumhoff
Dim i As Long, j As Long, k As Long, n As Long, lCount As Long, lSgn As Long
Dim d As Double, dDiff As Double, dRoundedSum As Double, dSumAbs As Double: Dim vA As Variant
With Application.WorksheetFunction
vA = .Transpose(.Transpose(vInput)): On Error GoTo Errhdl: i = vA(1) 'Force error in case of vertical arrays
On Error GoTo 0: n = UBound(vA): ReDim vC(1 To n) As Variant, vD(1 To n) As Variant: dSumAbs = .Sum(vA)
For i = 1 To n
  d = IIf(bAbsSum, vA(i), vA(i) / dSumAbs * 100#): vC(i) = .Round(d, lDigits)
  If lErrorType = 1 Then 'Absolute error
    vD(i) = vC(i) - d
  ElseIf lErrorType = 2 Then 'Relative error
    vD(i) = (vC(i) - d) * d
  Else
    RoundToSum = CVErr(xlErrValue): Exit Function
  End If
Next i
If Not bDontAmend Then
  dRoundedSum = .Round(IIf(bAbsSum, dSumAbs, 100#), lDigits)
  dDiff = .Round(dRoundedSum - .Sum(vC), lDigits)
  If dDiff <> 0# Then
    lSgn = Sgn(dDiff): lCount = .Round(Abs(dDiff) * 10 ^ lDigits, 0)
    'Now find highest (lowest) lCount indices in vD
    ReDim m(1 To lCount) As Long
    For i = 1 To lCount: m(i) = i: Next i
    For i = 1 To lCount - 1
      For j = i + 1 To lCount
        If lSgn * vD(m(i)) > lSgn * vD(m(j)) Then k = m(i): m(i) = m(j): m(j) = k
      Next j
    Next i
    For i = lCount + 1 To n
      If lSgn * vD(i) < lSgn * vD(m(lCount)) Then
        j = lCount - 1
        Do While j > 0
          If lSgn * vD(i) >= lSgn * vD(m(j)) Then Exit Do
          j = j - 1
        Loop
        For k = lCount To j + 2 Step -1: m(k) = m(k - 1): Next k: m(j + 1) = i
      End If
    Next i
    For i = 1 To lCount: vC(m(i)) = .Round(vC(m(i)) + dDiff / lCount, lDigits): Next i
  End If
End If
RoundToSum = vC
If TypeName(Application.Caller) = "Range" Then
  If Application.Caller.Rows.Count > Application.Caller.Columns.Count Then
    RoundToSum = .Transpose(vC) 'It's two-dimensional with 2nd dim const = 1
  End If
End If
Exit Function
Errhdl:
'Transpose variants to be able to address them with vA(i), not vA(i,1)
vA = .Transpose(vA): Resume Next
End With
End Function

Sub DescribeFunction_RoundToSum()
'Run this only once, then you will see this description in the function menu
Dim FuncName As String, FuncDesc As String, Category As String, ArgDesc(1 To 5) As String
FuncName = "RoundToSum"
FuncDesc = "Rounding values preserving their rounded sum"
Category = mcMath_and_Trig
ArgDesc(1) = "Range or array which contains unrounded values"
ArgDesc(2) = "[Optional = 2] Number of digits to round to. For example: 0 rounds to integers, 2 rounds to the cent, -3 will use thousands"
ArgDesc(3) = "[Optional = True] True takes the summands as they are; False works on the summands' percentages to make all percentages add up to 100% exactly"
ArgDesc(4) = "[Optional = 1] Error type: 1= absolute error, 2 = relative error"
ArgDesc(5) = "[Optional = False] True does not amend the rounded summands to match the rounded sum; False performs the calculation as described"
Application.MacroOptions _
  Macro:=FuncName, _
  Description:=FuncDesc, _
  Category:=Category, _
  ArgumentDescriptions:=ArgDesc
End Sub

Download

Please read my Disclaimer.

Plumhoff_Rounding_Values_Preserving_Their_Sum_with_RoundToSum.pdf [1,117 KB PDF file, open and use at your own risk]