Apportion a value across cells with VBA

Today, I want to share a really useful time-saving macro with you.  It’s a macro to apportion a value across cells.  Now that I’ve written it, I’m starting to gain the benefits, and hopefully, you can too.

Download the example file: Join the free Insiders Program and gain access to the example file used for this post.

File name: 0005 Apportion value across cells.zip

When to use the macro?

Let’s say you’ve got a list of individual items, which add up to $210.  Your boss wants to add an additional $21 for contingency.  How are you going to make the total equal $231?  There are a few options:

  • Add an adjustment line with +$21
  • Identify specific lines which can be increased to achieve the $231 total
  • Spread the $21 increase equally across all the cells
  • Apportion the $21 increase across each line item so that each item receives a fair proportion.

The VBA code below addresses the fourth option.  You could do this manually using formulas, which would be time-consuming.  Instead, the VBA method below takes just 2 seconds.

WARNING! – Macros cannot be undone.  Before using the code in this post, make sure it works the way you want.

The VBA code

Enter the following code into a standard module.

Sub ApportionValueAcrossCells()

Dim apportionValue As Double
Dim keepAsFormula As Long
Dim total As Double
Dim c As Range
Dim formulaString As String

'Get the existing total
total = Application.WorksheetFunction.Sum(Selection)

'Check that sum of selected cells is not zero
If total = 0 Then

    MsgBox Prompt:="Selected cells must not sum to zero", _
    Title:="Apportion value"
Exit Sub

End If

'Get the value to apportion
apportionValue = Application.InputBox(Prompt:="Value to apportion:", _
Title:="Apportion value", Type:=1)

'The User clicked Cancel
If apportionValue = False Then Exit Sub

'Get the boolean value to keep the formula or hardcode the result
keepAsFormula = MsgBox("Keep formula?", vbYesNo)

'Loop through each cell in selection
For Each c In Selection

    If IsNumeric(c.Value) Then

        'Calculate the result of the cell
        formulaString = c.Formula & "+(" & apportionValue & _
            "/" & total & "*" & c.Value & ")"

        If Left(formulaString, 1) <> "=" Then _
            formulaString = "=" & formulaString

        'Enter the formula into the cell
        c.Formula = formulaString

        'Recalculate the active cell
        ActiveCell.Calculate

        'If keepAsFormula is no, then hardcode the result
        If keepAsFormula = vbNo Then
            c.Value = c.Value
        End If

    End If

Next c

End Sub

How to use the code

To use the macro, follow these steps:

  1. Select the cells which contain the current values
    Select cells before running macro
  2. Run the macro
  3. An Input Box will appear, enter the value you wish to adjust the total by, then click OK
    Apportion a value across cells - input screen
  4. A message box will appear.  The macro can either paste the new values or include the formula.  Click on the button with your preference.
    Keep formula option message box
  5. Ta-dah! The values will be updated.  The 21 has now been apportioned across all the cells according to their original value.
    Cells after running macro

Help me improve the macro

I’ve written this code to operate the way that I work and to meet my needs.  However, maybe you work differently, maybe you can think of some improvements, or maybe you have a different way to apportion a value across cells.  If so, please include your thoughts in the comments section below.


Discover how you can automate your work with our Excel courses and tools.

Excel Academy

Excel Academy
The complete program for saving time by automating Excel.

Excel Automation Secrets

Excel Automation Secrets
Discover the 7-step framework for automating Excel.

Office Scripts Course

Office Scripts: Automate Excel Everywhere
Start using Office Scripts and Power Automate to automate Excel in new ways.

2 thoughts on “Apportion a value across cells with VBA”

  1. Umm… umm… a setup example that talks about a reduction of the total, followed by an example offers no explanation as to what the numbers mean and it’s about increasing the total. I know what apportioning means, which allows me to make heads and tails out of this article, but, with all due respect, that is not the way of go about that explanation, and it is much unlike your other articles that I have read so far. It seems like the job was delegated at the very last moment to a junior something, and it turned out like what my students who try to do their homework 10 minutes before class turn in.

    By the way, and please forgive me, either the boss wants the total to be reduced or the total needs to be reduced (for whatever reason there might be), but not a mix of both. It’s clear that it was an editing error, but for the sake of not confusing those with lesser English skills…

    Reply
    • Thanks for the feedback. Unfortunately, it was not delegated, but entirely me. Clearly, I must try harder! 🙂

      I have updated the article for some of the comments. This is a ‘code snippet’ article, so I’m not trying to explain how to apportion, I’m trying to provide a piece of code which can achieve it.

      Did you try the macro?

      Reply

Leave a Comment