I'm trying to have a formula somewhat like a sum function. The only difference is that once it sums up the amounts of 3 cells (or more) it will edit a certain cell to maintain that total value if another cell is edited.
For example:
Column A, B, and C are each 3 and together equal 9:
| A | B | C |...| Total |
+----+-----+----+...+-------+
| 3 | 3 | 3 |...| 9 |
Columns A and B are edited to equal 2 each, but I still want to maintain the total of 9, so I want column C to automatically change to 5.
this:
| A | B | C |...| Total |
+----+-----+----+...+-------+
| 2 | 2 | 3 |...| 7 |
should update to:
| A | B | C |...| Total |
+----+-----+----+...+-------+
| 2 | 2 | 5 |...| 9 |
The reasoning behind this is because it is being sent to multiple parts of the country where only 2 cells will be edited, but I want to maintain a total without having to edit 2k+ rows of data inputs.
I'm open to a VBA option as well. Anyone have any ideas?
You can find the problem here.
The example:
Column A, B, and C are each 3 and together equal 9:
Columns A and B are edited to equal 2 each, but I still want to maintain the total of 9, so I want column C to automatically change to 5:
I realized that my code was a bit of a mess, so I broke it into sheet1, module main and a class named
CollectionOfGeneratedValues
.Quick runthrough:
You have to set the
masterRange
, or the range that you are working with, inside VBA. Inside the spreadsheet you must set thesumtarget
for each row of themasterRange
.When a value is entered into a cell inside of your
masterRange
, we find out what row this is and generate a separate range that is just that row.If the input amount is greater than the
sumTarget
weExit Sub
and scold user.We generate an array of values whose sum, along with user input will be the
sumtarget
. We then take the sum target and subtract the user input.sumtarget.value
sumtarget
.columnsInRange
- 1 times.for loop
for the last value we set the value to whatever is leftover ofsumtarget
.Fisher-Yates Shuffle
, so that we don't always the values of collection / our spreadsheet come in a descending orderUPDATE: Thank you for your comments, advice and help. I should have given more thought what form looks like. I have been left with some sort of weird dynamic / static hybrid. A custom UserForm that generates sets of these and then can print into a worksheet would have been cool. Regardless, I took most of Raystafarian's advice. Some of it, like where to store input checking logic, I prefer my way. But generally his advice is spot on. Thank you again.
Sheet 1:
Module
SolveSudokuLite
:Option Explicit Sub Main(ByRef target As Range) Dim masterRange As Range Dim rangeToFill As Range Dim valuesToFillRange As GeneratedValuesCollection
Class named
GeneratedValuesCollection
:Class named
TargetSum
: