Random Team Pairings

excelbytes

Active Member
Joined
Dec 11, 2014
Messages
251
Office Version
  1. 365
Platform
  1. Windows
I have two groups #1 & #2. I need to create random pairs between the two groups, however I need to make sure that the pairings from Group #1 and Group #2 are not on the same team. Is there a way to do this?

Team Pairing.xlsm
ABCDE
1Group #1Group #2
2NumberTeamNumberTeam
31A70A
42A71A
53A72A
64A73A
75A74A
86A75A
97B76B
108B77B
119B78B
1210C79C
1311C80C
1412C81C
1513D82D
1614C83C
1715C84C
1816E85E
1917E86E
2018E87E
2119E88E
2220E89E
2321E90E
2422F91F
2523F92F
2624F93F
2725G94G
2826G95G
2927G96G
3028H97H
3129H98H
3230H99H
3331I100I
3432I101I
3533I102I
3634J103J
3735J104J
3836J105J
3937J106J
4038J107J
4139J108J
4240K109K
4341K110K
4442K111K
4543L112L
4644L113L
4745L114L
4846M115M
4947M116M
5048M117M
5149N118N
5250N119N
5351N120N
5452N121N
5553N122N
5654N123N
5755O124O
5856O125O
5957O126O
6058O127O
6159O128O
6260O129O
6361P130P
6462P131P
6563P132P
6664D133D
6765D134D
6866D135D
6967Q136Q
7068Q137Q
7169Q138Q
Sheet1
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
But how many ?! There are 69 pairs. I need to pair 1-69 with 70-138 randomly for team matchups, and will need to do this every week with a different pairing.

Any expected result ? See above.

Via a VBA procedure ? VBA would be fine. I would also be interested if there was a formula way to do this, but if that's not possible, then VBA.
 
Upvote 0
How about:

Book1 (version 1).xlsb
ABCDEFGH
1Group #1Group #2Pairs
2NumberTeamNumberTeamNumberNumber
31A70A3293
42A71A63111
53A72A774
64A73A54109
75A74A2289
86A75A6571
97B76B16138
108B77B45136
119B78B35117
1210C79C12128
1311C80C1976
1412C81C43137
1513D82D46120
1614C83C5096
1715C84C6985
1816E85E25104
1917E86E2199
2018E87E198
2119E88E36131
2220E89E48108
2321E90E3387
2422F91F40105
2523F92F68106
2624F93F20121
2725G94G6286
2826G95G3492
2927G96G6122
3028H97H5197
3129H98H5114
3230H99H49101
3331I100I37134
3432I101I1170
3533I102I26133
3634J103J58119
3735J104J42132
3836J105J28107
3937J106J3129
4038J107J6682
4139J108J23130
4240K109K14112
4341K110K1094
4442K111K6079
4543L112L8103
4644L113L3972
4745L114L3890
4846M115M61100
4947M116M53135
5048M117M31110
5149N118N2781
5250N119N67113
5351N120N1791
5452N121N2977
5553N122N13124
5654N123N9116
5755O124O2123
5856O125O59118
5957O126O5288
6058O127O4784
6159O128O1580
6260O129O2473
6361P130P56126
6462P131P57102
6563P132P18115
6664D133D483
6765D134D55127
6866D135D4195
6967Q136Q64125
7068Q137Q3078
7169Q138Q4475
Sheet13
Cell Formulas
RangeFormula
G3:H71G3=CHOOSE({1,2},SORTBY(A3:A71,RANDARRAY(69)),SORTBY(D3:D71,RANDARRAY(69)))
Dynamic array formulas.
 
Upvote 0
The challenge with this is the comment that I made "I need to make sure that the pairings from Group #1 and Group #2 are not on the same team". Your solution has several matches of the teams.
 
Upvote 0
I didn't understand that requirement. Give this a shot:

Book1
ABCDEFGHIJ
1Group #1Group #2Pairs
2NumberTeamNumberTeamNumberNumberPairs
31A70A112469
42A71A283
53A72A3112
64A73A482
75A74A5110
86A75A6126
97B76B787
108B77B8128
119B78B981
1210C79C10101
1311C80C11129
1412C81C12130
1513D82D1370
1614C83C14123
1715C84C1578
1816E85E1673
1917E86E17125
2018E87E18116
2119E88E19131
2220E89E20137
2321E90E21114
2422F91F22135
2523F92F2399
2624F93F2474
2725G94G2590
2826G95G26115
2927G96G27117
3028H97H2896
3129H98H2989
3230H99H30118
3331I100I3197
3432I101I32122
3533I102I33133
3634J103J3476
3735J104J35132
3836J105J3671
3937J106J3780
4038J107J3895
4139J108J3972
4240K109K4094
4341K110K4191
4442K111K42113
4543L112L4386
4644L113L4475
4745L114L4592
4846M115M4679
4947M116M47127
5048M117M48109
5149N118N4993
5250N119N50100
5351N120N5177
5452N121N52107
5553N122N53104
5654N123N54103
5755O124O55108
5856O125O56119
5957O126O57138
6058O127O5885
6159O128O5998
6260O129O60106
6361P130P61120
6462P131P6288
6563P132P6384
6664D133D64111
6765D134D65105
6866D135D66136
6967Q136Q67121
7068Q137Q68134
7169Q138Q69102
Sheet3
Cell Formulas
RangeFormula
J3J3=SUM(COUNTIF(D3:D71,H3:H71))
H3:H71H3=SMALL(IF((COUNTIF(H$2:H2,D$3:D$71)=0)*(B3<>E$3:E$71),D$3:D$71),RANDBETWEEN(1,SUM((COUNTIF(H$2:H2,D$3:D$71)=0)*(B3<>E$3:E$71))))
Press CTRL+SHIFT+ENTER to enter array formulas.


There is a chance that the last pair can't be assigned, due to how the algorithm works. I added the J3 formula to check that. If the number isn't 69, just press F9 again.
 
Upvote 0
Solution
Another option with VBA:
VBA Code:
Sub a1181792a()
Dim i As Long, j As Long, n As Long, qq As Long
Dim c As Range
Dim tx As String
Dim va
Dim d As Object
Dim e As Object


va = Range("A3", Cells(Rows.Count, "B").End(xlUp))
vb = Range("D3", Cells(Rows.Count, "E").End(xlUp))

Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
Set e = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
    For i = 1 To UBound(va, 1)
        d(va(i, 1)) = va(i, 2)
        e(vb(i, 1)) = vb(i, 2)
    Next
    
ReDim vc(1 To UBound(va, 1), 1 To 2)

For Each x In d.Keys
    Randomize:    qq = 0
    Do While d.Count > 0
    a = WorksheetFunction.RandBetween(0, e.Count - 1)
    If d(x) <> e.Items()(a) Then
        j = j + 1
        vc(j, 1) = x
        vc(j, 2) = e.Keys()(a)
        d.Remove x
        e.Remove e.Keys()(a)
        Exit Do
    End If
    qq = qq + 1: If qq > 2000 Then MsgBox "Too many loops, restart the Sub": Exit Sub
    Loop
Next

'RESULT
Range("G3").Resize(UBound(vc, 1), 2) = vc
End Sub
 
Upvote 0
I need to pair 1-69 with 70-138 randomly for team matchups, and will need to do this every week with a different pairing.
Just an observation, as I'm coming in late.

If Group 1 Member #1 was paired with Group 2 Member #74, say, two or even three weeks in a row, that would certainly be random. But probably not an acceptable solution?

This sounds more like the Social Golfer Problem, i.e. you want different (rather than random) pairings each week?
 
Upvote 0
I didn't understand that requirement. Give this a shot:

Book1
ABCDEFGHIJ
1Group #1Group #2Pairs
2NumberTeamNumberTeamNumberNumberPairs
31A70A112469
42A71A283
53A72A3112
64A73A482
75A74A5110
86A75A6126
97B76B787
108B77B8128
119B78B981
1210C79C10101
1311C80C11129
1412C81C12130
1513D82D1370
1614C83C14123
1715C84C1578
1816E85E1673
1917E86E17125
2018E87E18116
2119E88E19131
2220E89E20137
2321E90E21114
2422F91F22135
2523F92F2399
2624F93F2474
2725G94G2590
2826G95G26115
2927G96G27117
3028H97H2896
3129H98H2989
3230H99H30118
3331I100I3197
3432I101I32122
3533I102I33133
3634J103J3476
3735J104J35132
3836J105J3671
3937J106J3780
4038J107J3895
4139J108J3972
4240K109K4094
4341K110K4191
4442K111K42113
4543L112L4386
4644L113L4475
4745L114L4592
4846M115M4679
4947M116M47127
5048M117M48109
5149N118N4993
5250N119N50100
5351N120N5177
5452N121N52107
5553N122N53104
5654N123N54103
5755O124O55108
5856O125O56119
5957O126O57138
6058O127O5885
6159O128O5998
6260O129O60106
6361P130P61120
6462P131P6288
6563P132P6384
6664D133D64111
6765D134D65105
6866D135D66136
6967Q136Q67121
7068Q137Q68134
7169Q138Q69102
Sheet3
Cell Formulas
RangeFormula
J3J3=SUM(COUNTIF(D3:D71,H3:H71))
H3:H71H3=SMALL(IF((COUNTIF(H$2:H2,D$3:D$71)=0)*(B3<>E$3:E$71),D$3:D$71),RANDBETWEEN(1,SUM((COUNTIF(H$2:H2,D$3:D$71)=0)*(B3<>E$3:E$71))))
Press CTRL+SHIFT+ENTER to enter array formulas.


There is a chance that the last pair can't be assigned, due to how the algorithm works. I added the J3 formula to check that. If the number isn't 69, just press F9 again.
That works, thanks!
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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