Dominoes team selection randomly

Rmejia

New Member
Joined
May 11, 2017
Messages
7
I'm new on this and need help with a macro I need to create. The goal is to randomly select partners for dominoes matches from a list of names.
Details:
I will have a list of players in column "B", every day could be a different amount of players and different names. In column "A" those players will have a unique number. The macro should combine those players in pairs and those partners will go against another pair both pairs randomly selected. I will specify on a cell how many times each player will play on that day. The constraints are that:


  1. No two players should play together on a team for the second time until all combinations have been used, and so on.
  2. No two same teams should play against each other for the second time until all combinations have been used.
What I need back from the macro is in …

  • Column D: Team 1, (Player 1 number).
  • Column E: Team 1, (Player 2 number).
  • Column F: Team 2, (Player 1 number).
  • Column G: Team 2, (Player 2 number).
I only need the number since my plan is to get the name from the master list with a <code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; background-color: rgb(239, 240, 241); white-space: pre-wrap;">vlookup</code>. Each row will be a different match. If for example, I have 8 players and each will play 5 times, at the end, I should have 10 rows with the matches:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">8 players x 5 games per player =40/4 players each match =10
</code>Please let me know if you have any questions, Thanks.
 
i started making something... i tried the links to create unique combinations and it was difficult to deal with so i just made something that is much simpler...

Code:
Sub MatchMaker()
    Dim players(), teams(), matches(), team, p1, p2, p3, p4, match As String
    Dim playerRange, r As Range
    Dim i, playerUB, teamUB, matchUB As Long
    
    Set playerRange = Range(Range("A2"), Range("A2").End(xlDown))
    playerUB = playerRange.Count - 1
    ReDim players(playerUB)
    
    For i = 1 To playerRange.Count
        players(i - 1) = playerRange.Cells(i, 1).Value2
    Next i
    
    teamUB = -1
    For i = 0 To playerUB - 1
        For j = i + 1 To playerUB
            team = players(i) & players(j)
            playerRange.Cells(teamUB + 2, 1).Offset(0, 3).Value2 = team
            teamUB = teamUB + 1
            ReDim Preserve teams(teamUB)
            teams(teamUB) = team
        Next j
    Next i

    matchUB = -1
    For i = 0 To teamUB - 1
        For j = i + 1 To teamUB
            p1 = Left(teams(i), 1)
            p2 = Right(teams(i), 1)
            p3 = Left(teams(j), 1)
            p4 = Right(teams(j), 1)
            
            If Not (p1 = p3 Or p1 = p4 Or p2 = p3 Or p2 = p4) Then
                match = teams(i) & teams(j)
                playerRange.Cells(matchUB + 2, 1).Offset(0, 4).Value2 = match
                matchUB = matchUB + 1
                ReDim Preserve matches(matchUB)
                matches(matchUB) = match
            End If
        Next j
    Next i
End Sub

Xv3vhS2.png


i will add code to generate the matches for each player and deplete the counter for each player... so i should get a lit of matches where each player plays twice in my example

i have every possible match in column E so i just need to randomly choose from that list (random row selector)
 
Last edited:
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
fUGobnw.png


Code:
Sub MatchMaker()
    Dim players(), teams(), matches(), team, p1, p2, p3, p4, match As String
    Dim playerRange, r As Range
    Dim i, j, playerUB, teamUB, matchUB As Long
    
    Set playerRange = Range(Range("A2"), Range("A2").End(xlDown))
    playerUB = playerRange.Count - 1
    ReDim players(playerUB)
    
    For i = 1 To playerRange.Count
        players(i - 1) = playerRange.Cells(i, 1).Value2
    Next i
    
    teamUB = -1
    For i = 0 To playerUB - 1
        For j = i + 1 To playerUB
            team = players(i) & players(j)
            playerRange.Cells(teamUB + 2, 1).Offset(0, 3).Value2 = team
            teamUB = teamUB + 1
            ReDim Preserve teams(teamUB)
            teams(teamUB) = team
        Next j
    Next i

    matchUB = -1
    For i = 0 To teamUB - 1
        For j = i + 1 To teamUB
            p1 = Left(teams(i), 1)
            p2 = Right(teams(i), 1)
            p3 = Left(teams(j), 1)
            p4 = Right(teams(j), 1)
            
            If Not (p1 = p3 Or p1 = p4 Or p2 = p3 Or p2 = p4) Then
                match = teams(i) & teams(j)
                playerRange.Cells(matchUB + 2, 1).Offset(0, 4).Value2 = match
                matchUB = matchUB + 1
                ReDim Preserve matches(matchUB)
                matches(matchUB) = match
            End If
        Next j
    Next i
    
    Dim playerMatches(), matchups() As String
    Dim matchCounts(), p1Index, p2Index, p3Index, p4Index As Long
    Dim pmUB, k, matchCount, muUB, selectedMatch As Long
    
    ReDim matchCounts(playerRange.Count)
    
    For i = 0 To playerRange.Count - 1
        matchCounts(i) = playerRange.Cells(i + 1, 1).Offset(0, 1).Value2
    Next i
    
    muUB = -1
    
    For i = 0 To playerUB
        pmUB = -1
        
        'get all matches for player
        For j = 0 To matchUB
            If InStr(matches(j), players(i)) Then
                pmUB = pmUB + 1
                ReDim Preserve playerMatches(pmUB)
                playerMatches(pmUB) = matches(j)
            End If
        Next j
        
        matchCount = matchCounts(i)
        For j = 1 To matchCount
TryAgain:
            selectedMatch = WorksheetFunction.RandBetween(0, pmUB)
            
            match = playerMatches(selectedMatch)
            p1 = Left(match, 1)
            p2 = Right(Left(match, 2), 1)
            p3 = Left(Right(match, 2), 1)
            p4 = Right(match, 1)
            
            For k = 0 To playerUB
                If players(k) = p1 Then
                    If matchCounts(k) > 0 Then
                        p1Index = k
                        GoTo continue
                    Else
                        GoTo TryAgain
                    End If
                End If
                If players(k) = p2 Then
                    If matchCounts(k) > 0 Then
                        p2Index = k
                        GoTo continue
                    Else
                        GoTo TryAgain
                    End If
                End If
                If players(k) = p3 Then
                    If matchCounts(k) > 0 Then
                        p3Index = k
                        GoTo continue
                    Else
                        GoTo TryAgain
                    End If
                End If
                If players(k) = p4 Then
                    If matchCounts(k) > 0 Then
                        p4Index = k
                        GoTo continue
                    Else
                        GoTo TryAgain
                    End If
                End If
continue:
            Next k
            
            matchCounts(p1Index) = matchCounts(p1Index) - 1
            matchCounts(p2Index) = matchCounts(p2Index) - 1
            matchCounts(p3Index) = matchCounts(p3Index) - 1
            matchCounts(p4Index) = matchCounts(p4Index) - 1
            
            muUB = muUB + 1
            ReDim Preserve matchups(muUB)
            matchups(muUB) = playerMatches(selectedMatch)
            playerRange.Cells(muUB + 1, 1).Offset(0, 5).Value2 = matchups(muUB)
            
            pmUB = pmUB - 1
            For k = selectedMatch To pmUB
                playerMatches(k) = playerMatches(k + 1)
            Next k
            
            If pmUB > -1 Then ReDim Preserve playerMatches(pmUB)
        Next j
    Next i
End Sub
 
Upvote 0
try this code... ... it was able to take the list of values in A and select from them randomly in groups of 2 vs 2 and displays in D-G

ApUfWk1.png


Code:
Sub SelectRandomPlayers()
    Dim players() As String, selectedPlayers(3) As String
    Dim playerRange As Range
    Dim i As Long, c As Long, writeRow As Long, ub As Long, selectedIndex As Long, j As Long, ub2 As Long
    
    writeRow = 2
    
    Set playerRange = Range(Range("A2"), Range("A2").End(xlDown))
    c = playerRange.count
    ub = c - 1
    ReDim players(ub)
    
    For i = 0 To ub
        players(i) = playerRange.Cells(i + 1).Value2
    Next i
    
    Do
        For i = 0 To 3
            ub2 = UBound(players)
            selectedIndex = WorksheetFunction.RandBetween(0, ub2)
            selectedPlayers(i) = players(selectedIndex)
            
            For j = selectedIndex To ub2
                If j < ub2 Then players(j) = players(j + 1)
            Next j
            
            If ub2 > 0 Then ReDim Preserve players(ub2 - 1)
        Next i
        
        Range("D" & writeRow).Value2 = selectedPlayers(0)
        Range("E" & writeRow).Value2 = selectedPlayers(1)
        Range("F" & writeRow).Value2 = selectedPlayers(2)
        Range("G" & writeRow).Value2 = selectedPlayers(3)
        writeRow = writeRow + 1
    Loop While UBound(players) > 0
End Sub

Hello Cerfani, is it too much to ask that you add comments to the steps so I can try to understand what's going on the sub and work around it to get the other constraints? Thank you
 
Upvote 0
Hello Cerfani, is it too much to ask that you add comments to the steps so I can try to understand what's going on the sub and work around it to get the other constraints? Thank you

try the last code i posted (post #12) instead... it does everything... test it out first... it only works for single character names though... it could be modified to handle integers to identify players or even player names themselves

i will add some comment to the code titled Sub MatchMaker()
 
Last edited:
Upvote 0
try the last code i posted (post #12) instead... it does everything... test it out first... it only works for single character names though... it could be modified to handle integers to identify players or even player names themselves

i will add some comment to the code titled Sub MatchMaker()

Thanks,

I tried it and got an error on line 68:

For j = 1 To matchCount


"Type mismatch"

I tried it with numbers and letters as the player's id.

Where I specify how many times a player will play?

Thank you.
 
Upvote 0
Code:
Sub MatchMaker()
    Dim players(), teams(), matches(), team, p1, p2, p3, p4, match As String
    Dim playerRange, r As Range
    Dim i, j, playerUB, teamUB, matchUB As Long
    
    'getting range where all players are listed...
    'i will use this to reference when i need to write stuff
    Set playerRange = Range(Range("A2"), Range("A2").End(xlDown))
    playerUB = playerRange.count - 1
    ReDim players(playerUB)
    
    'i am loading all the player names into an array
    For i = 1 To playerRange.count
        players(i - 1) = playerRange.Cells(i, 1).Value2
    Next i
    
    'creating all the unique teams and loading into an array
    'if you have players, A, B, C, D and you need unique teams you see the pattern is...
    'AB, AC, AD ... then for B it starts with BC (you already had team BA when you created AB)
    'There are two loops here that accomplish this, step through and watch the team names it makes
    teamUB = -1
    For i = 0 To playerUB - 1
        For j = i + 1 To playerUB
            team = players(i) & players(j)
            playerRange.Cells(teamUB + 2, 1).Offset(0, 3).Value2 = team
            teamUB = teamUB + 1
            ReDim Preserve teams(teamUB)
            teams(teamUB) = team
        Next j
    Next i


    'creating every possible matchup and loading them into an array
    'i also check each player to make sure a player isnt on both teams
    'the loops are set up the same way as before because that same pattern is need to create all unique combos
    matchUB = -1
    For i = 0 To teamUB - 1
        For j = i + 1 To teamUB
            p1 = Left(teams(i), 1)
            p2 = Right(teams(i), 1)
            p3 = Left(teams(j), 1)
            p4 = Right(teams(j), 1)
            
            If Not (p1 = p3 Or p1 = p4 Or p2 = p3 Or p2 = p4) Then
                match = teams(i) & teams(j)
                playerRange.Cells(matchUB + 2, 1).Offset(0, 4).Value2 = match
                matchUB = matchUB + 1
                ReDim Preserve matches(matchUB)
                matches(matchUB) = match
            End If
        Next j
    Next i
    
    'this is part 2 of the sub... this is where it has to select the matches for the actual matchups
    
    Dim playerMatches(), matchups() As String
    Dim matchCounts(), p1Index, p2Index, p3Index, p4Index As Long
    Dim pmUB, k, matchCount, muUB, selectedMatch As Long
    
    'create array to store my match counts
    'i want to edit the value but not edit the sheet
    ReDim matchCounts(playerRange.count)
    
    'i start by getting all the match counts in column 2 next to each player
    'make sure match count is the same for every player... this code wont be able to finish
    'if for every player you put two matches, but then one player you put one match
    'it just isn't possible to satisfy those conditions, so use good values
    For i = 0 To playerRange.count - 1
        matchCounts(i) = playerRange.Cells(i + 1, 1).Offset(0, 1).Value2
    Next i
    
    muUB = -1
    
    'this loop is going to allow me to do something for every player in the array...
    'i basically need to go through each player and select the number of matches for him in the counters
    'because this code is setup to read a counter from each player, if you put in bad values...
    'this code will never finish... every player should use the same match count
    For i = 0 To playerUB
        pmUB = -1
        
        'get all matches for current player and save in array
        'we will randomly select from only matches that include the current player...
        'and not entire list
        For j = 0 To matchUB
            If InStr(matches(j), players(i)) Then
                pmUB = pmUB + 1
                ReDim Preserve playerMatches(pmUB)
                playerMatches(pmUB) = matches(j)
            End If
        Next j
        
        'now for the current players match count i will add a match to the matchup array
        matchCount = matchCounts(i)
        For j = 1 To matchCount
        
        'because you want to randomly select matchs AND satisfy some conditions i set this up to try again
        'it will randomly select a match that may exceed one players match limit
TryAgain:
            'getting index of selected match
            selectedMatch = WorksheetFunction.RandBetween(0, pmUB)
            
            'getting players from match
            'note this will only work with one character player names
            'code needs to be edited for other formats
            match = playerMatches(selectedMatch)
            p1 = Left(match, 1)
            p2 = Right(Left(match, 2), 1)
            p3 = Left(Right(match, 2), 1)
            p4 = Right(match, 1)
            
            'now check each players match count left and if they have games left...
            'then this matchup can be added to the matchup list/array
            For k = 0 To playerUB
                If players(k) = p1 Then
                    If matchCounts(k) > 0 Then
                        p1Index = k
                        GoTo continue
                    Else
                        GoTo TryAgain
                    End If
                End If
                If players(k) = p2 Then
                    If matchCounts(k) > 0 Then
                        p2Index = k
                        GoTo continue
                    Else
                        GoTo TryAgain
                    End If
                End If
                If players(k) = p3 Then
                    If matchCounts(k) > 0 Then
                        p3Index = k
                        GoTo continue
                    Else
                        GoTo TryAgain
                    End If
                End If
                If players(k) = p4 Then
                    If matchCounts(k) > 0 Then
                        p4Index = k
                        GoTo continue
                    Else
                        GoTo TryAgain
                    End If
                End If
continue:
            Next k
            
            'so if every player has matches left then decrement the counter in the array for the current player
            matchCounts(p1Index) = matchCounts(p1Index) - 1
            matchCounts(p2Index) = matchCounts(p2Index) - 1
            matchCounts(p3Index) = matchCounts(p3Index) - 1
            matchCounts(p4Index) = matchCounts(p4Index) - 1
            
            'write matchup on form
            muUB = muUB + 1
            ReDim Preserve matchups(muUB)
            matchups(muUB) = playerMatches(selectedMatch)
            playerRange.Cells(muUB + 1, 1).Offset(0, 5).Value2 = matchups(muUB)
            
            'remove the selected match from the player matches array so it wont get selected again
            'i remove from array, then shift everything over and resize the array
            pmUB = pmUB - 1
            For k = selectedMatch To pmUB
                playerMatches(k) = playerMatches(k + 1)
            Next k
            
            If pmUB > -1 Then ReDim Preserve playerMatches(pmUB)
        Next j
    Next i
End Sub
 
Upvote 0
Thanks,

I tried it and got an error on line 68:

For j = 1 To matchCount


"Type mismatch"

I tried it with numbers and letters as the player's id.

Where I specify how many times a player will play?

Thank you.

Make your sheet exactly like this, then try to test it...

ZIsOxOj.png
 
Upvote 0
I just tested it running from the personal workbook and there was problems but when I added it to a module for the workbook where the player names are then it works fine.

So do not run from the personal workbook.

I noticed sometimes it needs to Try Again a lot (see code when selecting random matches)

to eliminate that after every match you would need to go through the list of matches you are selecting from and remove any where a player has 0 matches left so that means another loop lol
 
Last edited:
Upvote 0

Forum statistics

Threads
1,226,468
Messages
6,191,202
Members
453,647
Latest member
daymond

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