VBA New Constraint - Not sure how to start

KevinZ

New Member
Joined
Apr 14, 2019
Messages
33
I am looking to add a new constraint to my query. I am not sure how to start setting this up.

I want to add a constraint to set a maximum number of times a value can be used in my solutions. My loop will run for 1 to a maximum of 150 times. This will be set prior to running the solution.

For example, I may want Row 1 ($A$2) to be included a maximum of 6 times, Row 2 ($A$3) 4 times, Row 3($A$4) - 6 times, row 4($A$5) - 2 times, etc up to the may number of rows (Generally less than 200). These can be set anywhere from 0 to the max number of solutions on an individual basis.

I am solving for 6 out of the up to 200 rows at a time and looping for each value, when the row has been used the maximum number of times, it should be removed from consideration for any remaining solves.

My "A" column is the change variable, I have 2 other columns that can be used for a unique identifier for each row. The Name (Column B) and an ID Column (Column C)

I'm not sure how to set up a counter for each row and if I need to set up some type of error notice if there are not enough variables to create all of the solutions based on the other constraints.

Note: I asked a similar question in a previous thread but did not receive any replies. Please let me know if this makes sense, if not, I can try to reword/clarify it.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I am assuming you are doing these calculations using VBA?
Is it some formula that takes each of the values in turn? Is only one variable used at a time, or does the formula use more than one variables (from several rows)?
 
Upvote 0
I am using VBA to calculate a max solution, then I will loop to find the next best solution and so on up to about 150 times. The formula is set to use only 6 values at a time. I want to limit the number of times each value can be used but each value can be set individually.

Foe example, I am maximizing Column F with limits on Column D. Column A changes from 0 to 1 in the solved solution. I will copy the value from Column C to a new spreadsheet. Then it will loop and look for the next best solve. I want to limit the number of times a value can be used so if I am running the loop 50 times, I may want the first value a maximum of 6 times, the second a maximum of 2 times, etc. The first and second may or may not be used at the same time, it would depend on what the solver comes back with.

Let me know if this makes sense or if you need any clarification.

[TABLE="width: 384"]
<colgroup><col width="64" style="width:48pt" span="6"> </colgroup><tbody>[TR]
[TD="width: 64, bgcolor: transparent"]Column A[/TD]
[TD="width: 64, bgcolor: transparent"][/TD]
[TD="width: 64, bgcolor: transparent"]Column C[/TD]
[TD="width: 64, bgcolor: transparent"]Column D[/TD]
[TD="width: 64, bgcolor: transparent"][/TD]
[TD="width: 64, bgcolor: transparent"]Column F[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]0[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]12472012[/TD]
[TD="bgcolor: transparent, align: right"]9000[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]231[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]0[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]12472013[/TD]
[TD="bgcolor: transparent, align: right"]8900[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]95[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]0[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]12472014[/TD]
[TD="bgcolor: transparent, align: right"]8800[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]204[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]0[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]12472015[/TD]
[TD="bgcolor: transparent, align: right"]8700[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]231[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]0[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]12472016[/TD]
[TD="bgcolor: transparent, align: right"]8600[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]207[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]0[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]12472017[/TD]
[TD="bgcolor: transparent, align: right"]8500[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]124[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]0[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]12472018[/TD]
[TD="bgcolor: transparent, align: right"]8400[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]242[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]0[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]12472019[/TD]
[TD="bgcolor: transparent, align: right"]8300[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]87[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]0[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]12472020[/TD]
[TD="bgcolor: transparent, align: right"]8200[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]194[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]0[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]12472021[/TD]
[TD="bgcolor: transparent, align: right"]8100[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]187[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
A simple (not the quickest) solution will be ta add a column where you put in the number of times a variable can be used. Then when the code runs, every time it wants to use a variable, it checks to see if the number-of-times cell for that variable is >0, if so use it and substract 1 from the number-of-times cell.

It will be a lot faster if you load this number-of-times column into an array and then do this checking and subtracting in the array, to minimise the read/writes to the sheet. But you can do that in the optimise phase after checking if it works.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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