Generate unique random team groupings multiple times

starl

Administrator
Joined
Aug 16, 2002
Messages
6,091
Office Version
  1. 365
Platform
  1. Windows
I have 2 teams - Team A and Team B. I need to randomly group one player from each team. These groupings need to happen multiple times, and none of the groupings can be repeated between those times. Nor can a player be listed more than once within a week

So in my example below, columns A & B are my teams and players. Columns D:F show sample groupings (D&E are not very random, F is just a formula I threw in, but as you see, it doesn't always work). F10 and F25 are highlighted in red because that's the same group up in two different weeks and that's not allowed. Nor would a repeat of a group within a week or a single player listed twice in a week (F7&F18). I don't care if the solution is a formula or code. Week1 & Week2 are not very random and using that logic to generate the rest of the weeks (I need 10 total), would not be very random.

I'm showing my formula because I had it there, but it isn't what I expect the solution to be (especially since it would only generate a single week and there's no comparison to previous weeks to ensure non-duplicates)

bellevue.xlsx
ABCDEF
1Team ATeam BWeek 1Week2Week3
2B1A1B1-A1B1-A2B14-A23
3B2A2B2-A2B2-A3B8-A26
4B3A3B3-A3B3-A4B16-A3
5B4A4B4-A4B4-A5B4-A14
6B5A5B5-A5B5-A6B3-A15
7B6A6B6-A6B6-A7B22-A16
8B7A7B7-A7B7-A8B19-A7
9B8A8B8-A8B8-A9B7-A6
10B9A9B9-A9B9-A10B24-A25
11B10A10B10-A10B10-A11B16-A21
12B11A11B11-A11B11-A12B7-A19
13B12A12B12-A12B12-A13B3-A23
14B13A13B13-A13B13-A14B10-A26
15B14A14B14-A14B14-A15B4-A11
16B15A15B15-A15B15-A16B12-A19
17B16A16B16-A16B16-A17-A9
18B17A17B17-A17B17-A18B22-A4
19B18A18B18-A18B18-A19B13-A18
20B19A19B19-A19B19-A20B23-A11
21B20A20B20-A20B20-A21B15-A25
22B21A21B21-A21B21-A22B8-A19
23B22A22B22-A22B22-A23-A22
24B23A23B23-A23B23-A24B24-A19
25B24A24B24-A24B24-A25B2-
26B25A25B25-A25B25-A26B4-A18
27B26A26B26-A26B26-A1B9-A6
Sheet1
Cell Formulas
RangeFormula
F2F2=OFFSET($A$1,RANDBETWEEN(2,27),0)&"-" &OFFSET($B$1,RANDBETWEEN(2,27),0)
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Maybe something like this.
VBA Code:
Sub MakePairings()
    Dim I As Long, J As Long
    Dim R As Range, rngColData As Range
    Dim S As String
    Dim WS As Worksheet

    Set WS = ActiveSheet
    With WS
        Set rngColData = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
    End With

    Application.ScreenUpdating = False
    rngColData.Offset(0, 2).Resize(, 3).ClearContents
    For Each R In rngColData
        For J = 2 To 4
            S = NewCombo(rngColData.Offset(0, J))

            I = 0
            Do While Not rngColData.Offset(0, J).Find(What:=S, LookAt:=xlWhole) Is Nothing
                S = NewCombo(rngColData.Offset(0, J))
                I = I + 1
                If I > 100 Then
                    Exit Sub                          'emergency exit
                End If
            Loop
            R.Offset(0, J).Value = S
        Next J
    Next R
    rngColData.Offset(0, 2).Resize(, 3).Replace What:="$", Replacement:="", LookAt:=xlPart
    Application.ScreenUpdating = True
End Sub

VBA Code:
Function NewCombo(rngColData As Range) As String
    Dim I As Long
    Dim Found As Range
    Dim P1 As String, P2 As String, S As String

    I = 0
    Do
        Set Found = Nothing
        P1 = "A" & Application.WorksheetFunction.RandBetween(1, 26) & "$"

        I = I + 1
        If I > 200 Then
            Exit Function                             'emergency exit
        End If
        Set Found = rngColData.Find(What:=P1, LookAt:=xlPart)
    Loop Until Found Is Nothing

    I = 0
    Do
        Set Found = Nothing
        P2 = "B" & Application.WorksheetFunction.RandBetween(1, 26) & "$"

        I = I + 1
        If I > 200 Then
            Exit Function                             'emergency exit
        End If
        Set Found = rngColData.Find(What:=P2, LookAt:=xlPart)
    Loop Until Found Is Nothing

    S = P1 & "-" & P2
    NewCombo = S
End Function

tmpzl.xlsm
ABCDE
1Team ATeam BWeek 1Week2Week3
2B1A1A21-B6A3-B14A22-B25
3B2A2A16-B8A6-B2A13-B2
4B3A3A6-B2A13-B20A2-B24
5B4A4A7-B25A16-B17A5-B14
6B5A5A13-B12A19-B26A21-B9
7B6A6A22-B4A5-B12A7-B10
8B7A7A11-B16A2-B3A8-B11
9B8A8A3-B3A23-B15A3-B23
10B9A9A12-B9A10-B9A15-B1
11B10A10A25-B17A11-B16A20-B7
12B11A11A15-B26A12-B10A25-B16
13B12A12A8-B13A1-B13A9-B22
14B13A13A2-B21A15-B24A11-B26
15B14A14A19-B11A7-B21A16-B3
16B15A15A24-B10A14-B18A24-B6
17B16A16A5-B15A21-B11A12-B13
18B17A17A23-B7A18-B8A14-B19
19B18A18A9-B14A22-B7A10-B8
20B19A19A17-B22A17-B5A1-B15
21B20A20A26-B23A4-B22A26-B17
22B21A21A4-B19A26-B1A19-B4
23B22A22A20-B1A9-B4A18-B5
24B23A23A1-B24A24-B6A6-B21
25B24A24A14-B5A20-B19A4-B20
26B25A25A18-B18A8-B23A17-B18
27B26A26A10-B20A25-B25A23-B12
Sheet1
 
Upvote 0
Thank for looking at this @rlv01, but the solution does have some duplicates between weeks (in red)

Book1
ABCDE
1Team ATeam BWeek 1Week2Week3
2B1A1A21-B6A3-B14A22-B25
3B2A2A16-B8A6-B2A13-B2
4B3A3A6-B2A13-B20A2-B24
5B4A4A7-B25A16-B17A5-B14
6B5A5A13-B12A19-B26A21-B9
7B6A6A22-B4A5-B12A7-B10
8B7A7A11-B16A2-B3A8-B11
9B8A8A3-B3A23-B15A3-B23
10B9A9A12-B9A10-B9A15-B1
11B10A10A25-B17A11-B16A20-B7
12B11A11A15-B26A12-B10A25-B16
13B12A12A8-B13A1-B13A9-B22
14B13A13A2-B21A15-B24A11-B26
15B14A14A19-B11A7-B21A16-B3
16B15A15A24-B10A14-B18A24-B6
17B16A16A5-B15A21-B11A12-B13
18B17A17A23-B7A18-B8A14-B19
19B18A18A9-B14A22-B7A10-B8
20B19A19A17-B22A17-B5A1-B15
21B20A20A26-B23A4-B22A26-B17
22B21A21A4-B19A26-B1A19-B4
23B22A22A20-B1A9-B4A18-B5
24B23A23A1-B24A24-B6A6-B21
25B24A24A14-B5A20-B19A4-B20
26B25A25A18-B18A8-B23A17-B18
27B26A26A10-B20A25-B25A23-B12
Sheet1
 
Upvote 0
But - after reviewing your code, I have a couple of ideas how this can be resolved! Thank you - I didn't know where to start, but you've given me something.
 
Upvote 0
VBA Code:
Sub MakePairings()
    Dim I As Long, J As Long
    Dim R As Range, rngColData As Range, rngSearch As Range
    Dim S As String
    Dim WS As Worksheet

    Set WS = ActiveSheet
    With WS
        Set rngColData = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
    End With

    Application.ScreenUpdating = False
    Set rngSearch = rngColData.Offset(0, 2).Resize(, 3)
    rngSearch.ClearContents
    For Each R In rngColData
        For J = 2 To 4
            S = NewCombo(rngColData.Offset(0, J))

            I = 0
            Do While Not rngSearch.Find(What:=S, LookAt:=xlWhole) Is Nothing
                S = NewCombo(rngColData.Offset(0, J))
                I = I + 1
                If I > 300 Then
                    Call MakePairings
                    Exit Sub                          'emergency exit
                End If
            Loop
            R.Offset(0, J).Value = S
        Next J
    Next R
    rngSearch.Replace What:="$", Replacement:="", LookAt:=xlPart
    Application.ScreenUpdating = True
End Sub

FWIW, your 'no duplicate pairings' is not going to hold up over more than say 3-4 weeks. The team sizes are too small. At some point you will have to switch to a non-random assignment system.
 
Upvote 0
FWIW, your 'no duplicate pairings' is not going to hold up over more than say 3-4 weeks. The team sizes are too small. At some point you will have to switch to a non-random assignment system.
hmm - I'll keep that in mind. thanks!
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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