This problem would have been a really good homework assignment for beginning programming. It requires a little explination, though I will try to brief:
I received an Excel spreadsheet containing various forms of revenue for a company. I was also given a summary report from a different system which was supposed to represent the same data. However, the categorizations of the data on the report were different from those on the spreadsheets and the totals weren't adding up. So I had to take the totals from the report and figure out which cells on the revenue spreadsheet were used to make that total.
To do this, I had to come up with every combination of the cells, get a sum of each combination, and see which ones matched the value I was looking for.
The VBA code below will allow you to select a column of cells on a spreadsheet, right click to activate the program, enter a value to be searched for, and it will return all combinations of the cells that can be summed to make that value.
What was most difficult about this, was coming up with an algorithm for giving me all possible combinations of the cells. Plus, I wanted to search for the smallest number of returning cells first before searching for combinations containing all cells. The GetComb function in the code at the bottom of this article works in the following manor:
Given the Cells: A, B, C, D, E, I started with looking for the all single items (in the first column) which required just 1 pointer value. I moved that pointer from left to right (follow it going down the column). Next, I found all combinations of 2 items (in the second column) which required 2 pointers. I moved the last pointer (labeled 2) step-by-step to the right. When the last pointer was all the way to the right, I stepped back to the first pointer and stepped it one to the right, and moved the last pointer to the next consecutive place (row:5 column:2). I then moved the last pointer again, step-by-step to the right.
This is the algorithm, repeated until all combinations are found.
Here is the VBA code I wrote to do this, Enjoy!
More...