Generating all possible combinations per macro

KaNi2015

New Member
Joined
Sep 30, 2014
Messages
2
Dear community, good morning.

I am new to this forum and hope you can help me.

Here is my problem.

Short version:

1. I ne
ed VBA to generate all possible combinations following a certain logic
2. Once a new combination is found, this combination needs to be evaluated with a certain function (that I already have)

Detailed version:

- I need to optimize the allocation of products to a shelf
- I need only two integer variables, number of products N and shelf-space (capacity) S
- I want VBA to generate the full list of all possible combinations of allocating products to the shelf and evaluate each combination with a certain function (that I already have)
- Assumption 1: I always need to fill the shelf completely
- Assumption 2: the order of the products does not matter (means only the total number per product counts, not where the product is placed on the shelf)
- here comes an example for N=3 product and S=3 shelf-spaces (which means that I can put a total of 3 products onto the shelf) - which gives in total 10 possible combinations

Quantity of product 1 Quantity of product 2 Quantity of product 3

0 0 3
0 1 2
0 2 1
0 3 0
1 0 2
1 1 1
1 2 0
2 0 1
2 1 0
3 0 0

I assume you need to build a kind of "recursive" loop for this. I have managed to write a non-flexible macro which can deal with a flexible number for S but I always need to build in a new loop whenever I want to add a product.

What I am looking for is a fully flexible macro such that I can just put S and N into two excel cells and the macro evaluates all combinations.

You do not need to worry about the evaluation function for each combination (to simplify, assume this is just the sum of the number of all products, which then of course would always equal S).

Please do let me know if you need more information - I hope the above is clear.

Thanks a lot in advance for your help.
Best regards

Kai
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,223,723
Messages
6,174,111
Members
452,544
Latest member
aush

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top