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
I recommend you download the files which support this post, as you’ll be able to work along with examples. This is the best way to learn. You’ll be able to see the solutions in action, plus the file will be useful for future reference. The support files are available for FREE to newsletter subscribers.
Click below to subscribe and gain access to the subscriber area. You will also receive:
- My favorite tips and tricks direct to your inbox
- Exclusive content (which is only available to subscribers)
- FREE tools and downloads
If you’re already a subscriber, click here to log-in to the subscriber downloads area.
The filename for this post is 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.
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:
- Select the cells which contain the current values
- Run the macro
- An Input Box will appear, enter the value you wish to adjust the total by, then click OK
- A message box will appear. The macro can either paste the new values or include the formula. Click on the button with your preference.
- Ta-dah! The values will be updated. The 21 has now been apportioned across all the cells according to their original value.
- Select the cells which contain the current values
Become a VLOOKUP master with the Advanced VLOOKUP Cheat Sheet
- Faster Calculation
- VLOOKUP to the left
- Automatically change the column number
- Lookup with multiple criteria
- Lookup with rows and columns
Download the VLOOKUP Cheat Sheet today!
Click the button below to subscribe, you’ll gain access to the subscriber area in which you can download the cheat sheet.
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.
Now that you’ve learned how to apportion a value across cells with VBA, you should check out the following posts, which will help you along your Excel journey. Don’t forget: If you’ve found this post useful, or if you have a better approach, then please leave a comment below. Do you need help adapting this to your needs? I’m guessing the examples in this post didn’t exactly meet your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs. But, if you’re still struggling you should:
If you’ve found this post useful, or if you have a better approach, then please leave a comment below.
Do you need help adapting this to your needs?
I’m guessing the examples in this post didn’t exactly meet your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs.
But, if you’re still struggling you should: