Challenge - looping a random list generator until a criteria is met

dkjonesau

New Member
Joined
May 9, 2014
Messages
46
Hi all,

I have a challenge that has me stumped.

I have a sheet with the entrants into an event. The rules say that the entrants order of competition must be randomly drawn.

I have this nearly nailed. I've got one step left....

If once all the entries are in I have a list that looks like this:
uc


Currently I have procedure which generates a random number in each line in column M.

If I don't have enough entries to fill the last squad (eg. Squad 3 here) I place a filler in the remaining boxes to make a complete squad, then I sort the list by the random numbers down to the last squad filled.

I may have to repeat the routine a number of times, until I have an even spread of "Filler". Eg in this case ideally only one to each squad.

I guess my worst case scenario above would be that squad 3 only has 2 in it, and 4 fillers. Then I'd need 2 squads of 5 & one of 4, instead of three squads of 5.

5 fillers and 13 shooters isn't out of the question but would be a pain.

Currently I manually repeat the macro below until one of the outcomes meets my criteria.

I know it's a tall ask but if someone is up to a weekend challenge I'd really like to know how to do the following:

1. Create the optimum mix of squads eg 1 filler in each squad in scenario in image OR 1 in each of first 2 squads and 2 in the third if I had another filler. (ie. run the sort, test to see if there's one filler in each squad, repeat the sort if not until the criteria is met - however it can't crash if the mix means that an extra filler is needed in one or two squads because entry numbers dictate it.)

2. I'd really like to end up with the fillers last in each squad. By giving them athlete number 9999 I'm figuring just sort at the end by number. I guess ultimately I'll need to select a range and run a sort on squad 1, move down 6 cells to select a new range to sort squad 2 and so on down to squad 15.

The current routine I'm using to randomise the list is:

' Select all competitors on qualification sheet for random sort.
' Requires last squad to contain "Filler" in each vacant cell.
Sub Select_Qual_athletes_for_sort()
Range("M7:M96").Formula = "=RAND()"
Range(Range("M7"), Range("M7").End(xlDown)).Select
Selection.Copy
Range("M7").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range(Range("D7"), Range("D7").End(xlDown)).Select
' Worksheets("Sheet1").Activate
numRows = Selection.Rows.Count
numColumns = Selection.Columns.Count
On Error Resume Next
Selection.Resize(, Selection.Columns.Count + 10).Offset(, -1).Select
With ActiveSheet.Sort
.SortFields.Clear
.SortFields.Add Key:=Selection.Columns(11), Order:=xlAscending
.Header = xlNo
.SetRange Selection
.Apply
End With
Range("A1").Select
Application.CutCopyMode = False
End Sub

Any ideas?

Thanks,

Dave
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Ignore the numbers that went to #VALUE or >9999. That won't normally happen. Just the way I created the sample data. Normally they'll be manually entered and won't be >3000.

First run at random list - no good one squad none, one has two, one has one.
uc


Eighth manual attempt - success. Want to be able to loop to get to this point...

uc



Cheers,

DJ
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,702
Members
449,048
Latest member
81jamesacct

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