VBA for Optimal Combinations with Multiple Constraints

Manny82

New Member
Joined
Jan 3, 2021
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I am new to excel VBA - but I do understand after I read the code. I have the attached spreadsheet where I have sheet "Combinations" with all possible combinations for P1-P5 items. While the best combination is ranked based on Value, I want to create an output for 52 weeks in a year using the constrains from "Constraints" sheet.

So for example P1 with a P_Val of 22 - in 52 weeks you can only have a total of between 29-31 weeks - this is while also optimising for the highest possible value. This is easy if we are looking at just one P - but in my case I have 5 Ps and its becoming hard. I have manually done a sample output in "Output' sheet - while it manages most of the constraints it doesn't work exactly. Any help to do this in VBA would be total awesome. Thanks heaps for help.

Combinations sheet:

combinations.xlsx
G
21
Combinations


Constraints Sheet

combinations.xlsx
B
222
Constraints


Output Sheet

combinations.xlsx
J
22
Output
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Sorry i didn't copy the image properly

Combinations Sheet:

combinations.xlsx
ABCDEFG
1P1P2P3P4P5ValueRank
21013.530128393690.11
31013.53010.58337355.32
41013.5251283347893
51013.530127324510.64
62213.514128283257.28
71011.530128297441.25
81013.520.5128288501.56
91013.52510.58287976.67
101013.53010.57278784.79
111013.52512727663610
121013.514128268755.711
132213.51410.58259611.814
141013.5308.58265838.312
151013.530126260994.613
162211.514128244223.719
172213.514127243927.120
181011.53010.58256361.215
191913.514128243227.421
201011.525128254166.216
211011.530127245930.318
221013.520.510.58249461.517
232213.51458221308.233
241013.51410.58235486.924
252213.5148.58226698.626
262211.51410.58224377.429
271013.520.5127239229.722
281013.52510.57238639.323
292213.51410.57224069.830
301913.51410.58222993.232
311013.514127225060.127
32229.514128209664.543
331013.5258.58228443.125
341013.53010.56224956.728
352211.514127210491.641
36109.530128218749.537
371013.525126223311.131
381911.514128209200.745
391011.520.512822077034
Combinations


Constraints Sheet:

combinations.xlsx
ABCD
1ItemP_ValFrequency_MinFrequency_Max
2P1222931
3P11946
4P1101115
5P11646
6P3302931
7P32046
8P3141115
9P32546
10P4122931
11P410.546
12P451115
13P48.546
14P213.52931
15P211.546
16P27.51115
17P29.546
18P582931
19P5746
20P551115
21P5646
Constraints


Output Sheet:

combinations.xlsx
ABCDEFGHIJKLMNOPQRST
1WEEKP1P2P3P4P5ValueHALFP1CountP2CountP3CountP4CountP5Count
211013.530125201749.46461H1H221613.51530141215814
32227.514128169083.08391H1H19111.5225210.5272
431013.530125201749.46461H1H1629.5220.528.5263
54227.514128169083.08391H1H1077.571485757
652211.5308.5877018.124661H2626262626
76227.514128169083.08391H
871013.530125201749.46461H2H221413.51630161215817
98227.514128169083.08391H2H19311.5225110.5272
1091013.530125201749.46461H2H1629.5120.528.5260
1110227.514128169083.08391H2H1077.571475757
12111013.530125201749.46461H2626262626
1312227.5305896142.778641H
14131913.514128243227.44571H
1514229.52510.5880309.378151H
16152213.51457196220.18931H
17161013.530125201749.46461H
18172213.51457196220.18931H
19181613.520.5128108877.90451H
20192213.53056112269.16661H
21201613.520.5128108877.90451H
22212211.5308.5877018.124661H
23222213.53056112269.16661H
2423227.5305896142.778641H
25241013.530125201749.46461H
26252213.53056112269.16661H
2726229.52510.5880309.378151H
28271013.530125201749.46462H
29282211.5308.5877018.124662H
30291013.530125201749.46462H
3130227.53012871630.992692H
32312213.51457196220.18932H
33321013.530125201749.46462H
3433227.53012871630.992692H
35342213.51457196220.18932H
36351013.530125201749.46462H
3736227.53012871630.992692H
38372213.51458221308.17032H
39381013.530125201749.46462H
4039227.5305896142.778642H
41401913.514128243227.44572H
4241227.5305896142.778642H
43421913.514128243227.44572H
4443227.5305896142.778642H
45441613.520.5128108877.90452H
46452213.51410.58259611.80272H
47461013.530125201749.46462H
4847227.5305896142.778642H
49481613.520.5128108877.90452H
50491913.514128243227.44572H
51502211.5308.5877018.124662H
5251229.52510.5880309.378152H
53521013.530125201749.46462H
Output
Cells with Conditional Formatting
CellConditionCell FormatStop If True
S1Expression=(#REF!="Base")textNO
S1Expression=(#REF!="Deep")textNO
S1Expression=(#REF!="Shallow")textNO
S1Expression=(#REF!="HP")textNO
Q1Expression=(#REF!="Base")textNO
Q1Expression=(#REF!="Deep")textNO
Q1Expression=(#REF!="Shallow")textNO
Q1Expression=(#REF!="HP")textNO
O1Expression=(#REF!="Base")textNO
O1Expression=(#REF!="Deep")textNO
O1Expression=(#REF!="Shallow")textNO
O1Expression=(#REF!="HP")textNO
M1Expression=(#REF!="Base")textNO
M1Expression=(#REF!="Deep")textNO
M1Expression=(#REF!="Shallow")textNO
M1Expression=(#REF!="HP")textNO
K1Expression=(#REF!="Base")textNO
K1Expression=(#REF!="Deep")textNO
K1Expression=(#REF!="Shallow")textNO
K1Expression=(#REF!="HP")textNO
I1Expression=(R1="Base")textNO
I1Expression=(R1="Deep")textNO
I1Expression=(R1="Shallow")textNO
I1Expression=(R1="HP")textNO
I1Expression=(AJ1="Base")textNO
I1Expression=(AJ1="Deep")textNO
I1Expression=(AJ1="Shallow")textNO
I1Expression=(AJ1="HP")textNO
A1Expression=(#REF!="Base")textNO
A1Expression=(#REF!="Deep")textNO
A1Expression=(#REF!="Shallow")textNO
A1Expression=(#REF!="HP")textNO
T1Expression=(AB1="Base")textNO
T1Expression=(AB1="Deep")textNO
T1Expression=(AB1="Shallow")textNO
T1Expression=(AB1="HP")textNO
R1Expression=(Z1="Base")textNO
R1Expression=(Z1="Deep")textNO
R1Expression=(Z1="Shallow")textNO
R1Expression=(Z1="HP")textNO
P1Expression=(X1="Base")textNO
P1Expression=(X1="Deep")textNO
P1Expression=(X1="Shallow")textNO
P1Expression=(X1="HP")textNO
N1Expression=(V1="Base")textNO
N1Expression=(V1="Deep")textNO
N1Expression=(V1="Shallow")textNO
N1Expression=(V1="HP")textNO
L1Expression=(T1="Base")textNO
L1Expression=(T1="Deep")textNO
L1Expression=(T1="Shallow")textNO
L1Expression=(T1="HP")textNO
B1:F53Expression=(#REF!="Base")textNO
B1:F53Expression=(#REF!="Deep")textNO
B1:F53Expression=(#REF!="Shallow")textNO
B1:F53Expression=(#REF!="HP")textNO
 
Upvote 0

Forum statistics

Threads
1,215,044
Messages
6,122,827
Members
449,096
Latest member
Erald

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