Creating exception to random Team generator

paulgunther

New Member
Joined
Oct 30, 2019
Messages
31
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet that assigns players to random holes (Start) based on a chosen number of players per card (I3). It's fairly simple and I also have a macro assigned to a button that copies the "random" column and pastes values to the "static" column to re-randomize at will. The "hole_assignment" table is to assign the holes in a specific, non-sequential order. This works fine as designed.

Question: I would like to occasionally assign certain players to specific holes (column A) and have the Start column account for the pre-assigned starting hole and randomize the rest.

Example: Player 2, pre-assigned hole 8, Player 6 pre-assigned hole 8. I now need the randomizer to only assign 2 additional players to hole 8 and automatically place those players on their respective pre-assigned holes.

Random Card.xlsm
ABCDEFGHI
1
2Non RandomCardPlayerStartGroupRandomStatic
3Player 1270.2263043540.262627438Card Size4
48Player 2820.3879379380.796961803
5Player 31030.6701634850.68759244CardAssigned Hole
6Player 4460.5389380970.35194473911
7Player 51030.6408439280.74684021828
88Player 61030.8737932760.75425269310
9Player 71240.5654047050.627886504412
10Player 8110.4988491780.87951887256
11Player 99100.0932334210.04976329364
12Player 101390.797314870.09855282472
13Player 11110.6819009280.889456719811
14Player 12650.7408938520.569148347913
15Player 13110.340686960.865395664109
16Player 14460.9233135340.369695062113
17Player 151390.7531950940.13534933127
18Player 161180.8870338330.2111415521318
19Player 17460.6603175250.517424354145
20Player 18650.7532726010.554959501
21Player 19650.5153775270.524942895
22Player 201180.2742464570.256438207
23Player 211390.4251237450.149393574
24Player 229100.631862220.026967303
25Player 239100.341396670.096754073
26Player 24460.8051692220.388082165
27Player 251240.4998070020.615633054
28Player 261180.7721284090.217260568
29Player 27270.723632090.292360797
30Player 28110.167612520.905538699
31Player 29270.4967091590.270637506
32Player 30650.1722856520.564299634
33Player 31270.1871673010.261168275
34Player 32820.4636778770.784721414
35Player 33820.3399543980.782527076
36Player 34820.5471294770.796425025
37Player 351180.3305127370.201663754
38Player 361030.7042708740.764741023
39Player 371240.1209793360.597508326
40Player 381240.620319410.639348663
41Player 391390.9444481480.195836014
42Player 409100.7291462530.047685875
WORKING
Cell Formulas
RangeFormula
C3:C42C3=VLOOKUP([@Group],Hole_Assignment,2)
D3:D42D3=CEILING(RANK([@Static],[Static])/$I$3,1)
E3:F42E3=RAND()
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A3:A42Expression=COUNTIF($A$3:$A$42, A3) = 2textNO
A3:A42Expression=COUNTIF($A$3:$A$42, A3) = 3textNO
A3:A42Expression=COUNTIF($A$3:$A$42, A3) = 4textNO
C3:C42Expression=COUNTIF($C$3:$C$42, C3) = 2textNO
C3:C42Expression=COUNTIF($C$3:$C$42, C3) = 3textNO
C3:C42Expression=COUNTIF($C$3:$C$42, C3) = 4textNO
C3:C42Expression=COUNTIF($C$3:$C$42, C3) = 5textNO
C3:C42Expression=COUNTIF($C$3:$C$42, C3) > 5textNO
B3:B42Cell ValueduplicatestextNO
Cells with Data Validation
CellAllowCriteria
B2Any value
C3:D42Any value
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Holes
VBA Code:
Sub Random()
     Dim SCA, i, r, k, s, LO_P, LO_HA, Arr, Arr_P, Arr_HA, Res

     t = Timer
     Set SCA = CreateObject("system.collections.arraylist")
     Set LO_P = Sheets("Blad1").ListObjects("TBL_Players")     '
     Arr_P = LO_P.DataBodyRange
     col2 = LO_P.DataBodyRange.Columns(2)
     ReDim Res(1 To UBound(Arr_P), 1 To 1)

     Arr = Sheets("Blad1").ListObjects("Hole_Assigment").DataBodyRange
     Arr_HA = Arr
     col1 = Application.Index(Arr, 0, 1)     '1st column = cards
     ReDim Preserve Arr_HA(1 To UBound(Arr), 1 To UBound(Arr, 2) + 1)

     For i = 1 To UBound(Arr_P)     'all the players with a pre-assigned card
          If Len(Arr_P(i, 1)) > 0 Then
               r = Application.Match(Arr_P(i, 1), col1, 0)     'find card in 1st col
               If IsNumeric(r) Then
                    Arr_HA(r, UBound(Arr_HA, 2)) = Arr_HA(r, UBound(Arr_HA, 2)) + 1     'increment number of players with that card
                    Res(i, 1) = Arr_P(i, 1)     'assign player to that card
               Else
                    MsgBox "card not found, ERROR"
                    Res(i, 1) = "????"     'assign player to card "???"
               End If
          Else
               SCA.Add Arr_P(i, 2)     'arraylist of FREE players
          End If
     Next

     Do While SCA.Count     'as long as there are FREE players
          b = False
          i = WorksheetFunction.RandBetween(1, SCA.Count)      'take a random still Free player
          s = SCA.Item(i - 1)
          Arr = Evaluate("column(A1:Z1)")     'serie 1 to 26 (> number of cards
          For j = UBound(Arr_HA) To 1 Step -1     'choice a random card
               k = WorksheetFunction.RandBetween(1, j)     'take a random Free player
               l = Arr(k)
               If Arr_HA(l, UBound(Arr_HA, 2)) >= Range("Card_Size").Value Then
                    Arr(k) = Arr(j)
               Else
                    r = Application.Match(SCA.Item(i - 1), col2, 0)
                    If IsNumeric(r) Then Res(r, 1) = Arr_HA(l, 1)
                    Arr_HA(l, UBound(Arr_HA, 2)) = Arr_HA(l, UBound(Arr_HA, 2)) + 1     'increment number of players with that card
                    SCA.Remove SCA.Item(i - 1)
                    b = True
                    Exit For
               End If
          Next
          If Not b Then
               SCA.Remove SCA.Item(i - 1)
          End If
     Loop

     LO_P.ListColumns("Group").DataBodyRange.Value = Res
     'MsgBox Timer - t
End Sub
 
Upvote 0
Holes
VBA Code:
Sub Random()
     Dim SCA, i, r, k, s, LO_P, LO_HA, Arr, Arr_P, Arr_HA, Res

     t = Timer
     Set SCA = CreateObject("system.collections.arraylist")
     Set LO_P = Sheets("Blad1").ListObjects("TBL_Players")     '
     Arr_P = LO_P.DataBodyRange
     col2 = LO_P.DataBodyRange.Columns(2)
     ReDim Res(1 To UBound(Arr_P), 1 To 1)

     Arr = Sheets("Blad1").ListObjects("Hole_Assigment").DataBodyRange
     Arr_HA = Arr
     col1 = Application.Index(Arr, 0, 1)     '1st column = cards
     ReDim Preserve Arr_HA(1 To UBound(Arr), 1 To UBound(Arr, 2) + 1)

     For i = 1 To UBound(Arr_P)     'all the players with a pre-assigned card
          If Len(Arr_P(i, 1)) > 0 Then
               r = Application.Match(Arr_P(i, 1), col1, 0)     'find card in 1st col
               If IsNumeric(r) Then
                    Arr_HA(r, UBound(Arr_HA, 2)) = Arr_HA(r, UBound(Arr_HA, 2)) + 1     'increment number of players with that card
                    Res(i, 1) = Arr_P(i, 1)     'assign player to that card
               Else
                    MsgBox "card not found, ERROR"
                    Res(i, 1) = "????"     'assign player to card "???"
               End If
          Else
               SCA.Add Arr_P(i, 2)     'arraylist of FREE players
          End If
     Next

     Do While SCA.Count     'as long as there are FREE players
          b = False
          i = WorksheetFunction.RandBetween(1, SCA.Count)      'take a random still Free player
          s = SCA.Item(i - 1)
          Arr = Evaluate("column(A1:Z1)")     'serie 1 to 26 (> number of cards
          For j = UBound(Arr_HA) To 1 Step -1     'choice a random card
               k = WorksheetFunction.RandBetween(1, j)     'take a random Free player
               l = Arr(k)
               If Arr_HA(l, UBound(Arr_HA, 2)) >= Range("Card_Size").Value Then
                    Arr(k) = Arr(j)
               Else
                    r = Application.Match(SCA.Item(i - 1), col2, 0)
                    If IsNumeric(r) Then Res(r, 1) = Arr_HA(l, 1)
                    Arr_HA(l, UBound(Arr_HA, 2)) = Arr_HA(l, UBound(Arr_HA, 2)) + 1     'increment number of players with that card
                    SCA.Remove SCA.Item(i - 1)
                    b = True
                    Exit For
               End If
          Next
          If Not b Then
               SCA.Remove SCA.Item(i - 1)
          End If
     Loop

     LO_P.ListColumns("Group").DataBodyRange.Value = Res
     'MsgBox Timer - t
End Sub
Thanks BSALV. I am not even close to this level of programming. Do you have any explanations to accompany this code?
 
Upvote 0
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Thanks for that.
A formula option, although I feel there must be a simpler way.
Fluff.xlsm
ABCDEFGHIJ
1
2Non RandomCardPlayerStartGroupRandomStatic
3Player 1460.0456084390.505865Card Size4
48Player 21180.6190937790.474071
5Player 39100.4578123610.011008CardAssigned Hole
6Player 4460.6141299080.44786111
7Player 51180.2190623970.14991528
88Player 61180.9662408720.235066310
9Player 71240.4441067330.562627412
10Player 81030.2868645490.68880556
11Player 9820.2344608270.76699864
12Player 10650.4347368940.54427972
131Player 11110.7884732280.312539811
14Player 121030.4154417280.745614913
15Player 131390.8293831170.121125109
16Player 141390.2903437240.094565113
17Player 15110.1681011740.895886127
18Player 16270.2542893740.2609771318
191Player 17110.964485650.021962145
20Player 181390.5113176740.144665
21Player 19820.2338412990.779062
22Player 201240.4007961270.563879
23Player 21820.1635157330.801686
24Player 221180.1030761880.151359
25Player 231030.9544183970.605264
26Player 24460.538478960.533054
27Player 25650.8116453240.544648
28Player 261390.8199192820.057422
29Player 27270.5998977710.175729
30Player 28820.2861594230.788707
31Player 299100.1620015630.025451
32Player 309100.1864658760.053017
33Player 31650.4635601530.53648
34Player 32110.9971639410.864358
35Player 331240.1919129930.560422
36Player 34270.5595397580.28677
37Player 359100.5022810510.028894
38Player 36270.5045871860.305232
39Player 371240.7833257660.591905
40Player 381030.5225512860.618142
41Player 39650.7291530570.540354
42Player 40460.1205293060.400991
Sheet3
Cell Formulas
RangeFormula
C3:C42C3=VLOOKUP([@Group],Hole_Assignment,2)
D3:D42D3=LET(x,[Non RandomCard],f,FILTER(x*$J$3,x<>""),tr,ROWS(x),r,ROWS(f),s,f-MMULT((f=TRANSPOSE(f))*(SEQUENCE(r)>=SEQUENCE(,r)),SEQUENCE(r,,,0))+1,IF([@[Non RandomCard]]<>"",[@[Non RandomCard]],INDEX(FILTER(INT(SEQUENCE(tr,,,1/$J$3)),ISNA(MATCH(SEQUENCE(tr),s,0))),XMATCH([@Static],SORT(FILTER([Static],x=""),,-1),0))))
E3:E42E3=RAND()
Named Ranges
NameRefers ToCells
Hole_Assignment=Sheet3!$I$5:$J$19C3:C42
 
Upvote 0
i added additional explanation in the macro in previous link.
 
Upvote 0
Thanks Fluff,
I am definitely more familiar with formulas then VBA. This comes very close, but my goal is to match the "Start" column not the "group" column which gets very tricky since that value is determined by the lookup table of the variable in question. I hope that makes sense.


@BSALV : Thanks to you as well. Pardon my ignorance, but where do I put this code and how do I assign it as a Macro?
 
Upvote 0
Do you mean that the "Non RandomCard" is the "Assigned Hole" & not the "Card"?
 
Upvote 0
If the answer to my question is yes, then try
Fluff.xlsm
ABCDEFGHIJ
1139
2Non RandomCardPlayerStartGroupRandomStatic
3Player 1460.904638920.5058647Card Size4
48Player 28 0.072664690.47407113
5Player 39100.28429610.01100784CardAssigned Hole
6Player 4460.047779830.4478608211
7Player 51180.03794950.1499154828
88Player 68 0.443587990.23506583310
9Player 71240.844208990.56262691412
10Player 81030.528305690.6888050256
11Player 9820.229430240.7669975864
12Player 10650.446647540.5442785672
1313Player 1113 0.09105490.3125385811
14Player 121030.928140330.74561371913
15Player 131180.900596530.12112482109
16Player 141390.923992980.09456506113
17Player 15110.669798670.89588577127
18Player 16270.510235610.260977151318
1913Player 1713 0.981962140.02196248145
20Player 181180.17253430.14466525
21Player 19820.205365830.77906214
22Player 201240.057123880.56387949
23Player 21110.768038450.80168604
24Player 221180.288559940.15135902
25Player 231030.404058540.60526428
26Player 24460.485631440.53305447
27Player 25650.710709560.54464767
28Player 261390.777371740.05742175
29Player 27270.27156480.17572865
30Player 28110.196912260.78870732
31Player 299100.331752660.02545128
32Player 309100.867145290.05301655
33Player 31650.003242940.53647952
34Player 32110.792097950.86435832
35Player 331240.728799550.56042211
36Player 34270.741500560.2867701
37Player 359100.084097750.02889428
38Player 36270.753230170.30523173
39Player 371240.294463060.591905
40Player 381030.976092220.61814166
41Player 39650.97509850.54035428
42Player 40460.657125120.40099069
Original
Cell Formulas
RangeFormula
C3:C42C3=IF([@[Non RandomCard]]<>"",[@[Non RandomCard]],VLOOKUP([@Group],Hole_Assignment,2))
D3:D42D3=LET(x,[Non RandomCard],y,INDEX(Card,MATCH(x,Assigned_Hole,0)),f,FILTER(y*$J$3,ISNUMBER(y)),tr,ROWS(x),r,ROWS(f),s,f-MMULT((f=TRANSPOSE(f))*(SEQUENCE(r)>=SEQUENCE(,r)),SEQUENCE(r,,,0))+1,IF([@[Non RandomCard]]<>"","",INDEX(FILTER(INT(SEQUENCE(tr,,,1/$J$3)),ISNA(MATCH(SEQUENCE(tr),s,0))),XMATCH([@Static],SORT(FILTER([Static],x=""),,-1),0))))
E3:E42E3=RAND()
Named Ranges
NameRefers ToCells
Assigned_Hole=Original!$J$6:$J$19C3:D42
Card=Original!$I$6:$I$19C3:D42
Hole_Assignment=Original!$I$6:$J$19C3:D42
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,509
Members
448,967
Latest member
screechyboy79

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