Randomize seating problem

LenaH

New Member
Joined
May 21, 2020
Messages
40
Office Version
  1. 2016
Platform
  1. Windows
Dear all,

i have a list of Clubs, as shown below. I would like to avoid having same players from the same clubs sit together.
Is there a way to randomize it?

A table of players usually consists of 4 players. Of course, you can't avoid it fully, but a little randomization would surely help.

Tourney.xlsm
NO
1PlayerClub
2Player 1LA
3Player 2LA
4Player 3DE
5Player 4LA
6Player 5LA
7Player 6LA
8Player 7LA
9Player 8LA
10Player 9DE
11Player 10DE
12Player 11DE
13Player 12DE
14Player 13DE
15Player 14DE
16Player 15DE
17Player 16DE
18Player 17DE
19Player 18DE
20Player 19LV
21Player 20LV
22Player 21LV
23Player 22LV
24Player 23LV
25Player 24LV
26Player 25LV
27Player 26LV
28Player 27LV
29Player 28LV
30Player 29LV
31Player 30MN
32Player 31MN
33Player 32MN
34Player 33MN
35Player 34CANM
PlayerPool



Any ideas?
Lena
 
If there are 4 chairs per table and we know how many total players there are, the number of tables is know.

To distribute the players at those tables to keep team members apart, have the players line up by teams. All of Team A, followed by all of Team B, etc.
The first person in line sits at the first table, the second the second, etc until we reach the end of the tables and then loop back to Table 1.
This seperates all the team members from each other.

To randomize that process, randomize the order of the teams and the order of the players within each team.

VBA Code:
Sub test()
    Dim rngData As Range, oneCell As Range
    Dim arrTeams() As String, arrPlayers() As String
    Dim i As Long, j As Long, randIndex As Long, temp As Variant
    
    Dim Pointer As Long, playerPointer As Long, teamStartPointer As Long
    Dim PlayersCount As Long
    Dim Tables() As String
    Dim TablesCount As Long, ChairsPerTable As Long
    Dim tableIndex As Long, ChairIndex As Long
    ChairsPerTable = 4
    
    With Sheet1.Range("A:A")
        Set rngData = Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp))
    End With
    
    PlayersCount = rngData.Rows.Count
    ReDim arrTeams(1 To PlayersCount)
    ReDim arrPlayers(1 To PlayersCount)
    
    Rem make array of unique team names
    Pointer = 0
    For Each oneCell In rngData
        If IsError(Application.Match(oneCell.Offset(0, 1).Value, arrTeams, 0)) Then
            Pointer = Pointer + 1
            arrTeams(Pointer) = oneCell.Offset(0, 1).Value
        End If
    Next oneCell
    ReDim Preserve arrTeams(1 To Pointer)
    
    Rem rand reorder teams
    For i = 1 To Pointer
        randIndex = Int(Rnd() * Pointer) + 1
        temp = arrTeams(randIndex)
        arrTeams(randIndex) = arrTeams(i)
        arrTeams(i) = temp
    Next i
    
    Rem line the players up, all team members together
    playerPointer = 0
    For i = 1 To Pointer

        Rem add players from one team
        teamStartPointer = 0
        For Each oneCell In rngData
            With oneCell
                If oneCell.Offset(0, 1).Value = arrTeams(i) Then
                    playerPointer = playerPointer + 1
                    If teamStartPointer = 0 Then teamStartPointer = playerPointer
                    arrPlayers(playerPointer) = oneCell.Value
                End If
           End With
        Next oneCell
        
        Rem re-order the players of the new team
        For j = teamStartPointer To playerPointer
            randIndex = Int(Rnd() * (playerPointer - teamStartPointer + 1)) + teamStartPointer
            temp = arrPlayers(randIndex)
            arrPlayers(randIndex) = arrPlayers(i)
            arrPlayers(i) = temp
        Next j
    Next i
    
   Rem distribute among the tables
    TablesCount = Application.RoundUp(PlayersCount / ChairsPerTable, 0)
    ReDim Tables(1 To TablesCount, 1 To ChairsPerTable)
    tableIndex = 1: ChairIndex = 1
    
    For i = 1 To PlayersCount
        Tables(tableIndex, ChairIndex) = arrPlayers(i)
        tableIndex = tableIndex + 1
        If TablesCount < tableIndex Then
            tableIndex = 1
            ChairIndex = ChairIndex + 1
        End If
    Next i
    
    Rem output results
    With Range("G:G")
        For i = 1 To TablesCount
            With .Cells(Rows.Count, 1).End(xlUp).Offset(2, 0)
                For j = 1 To ChairsPerTable
                    .Cells(1, j).Value = Tables(i, j)
                Next j
            End With
        Next i
    End With
End Sub
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Rick,

=INT(N/4) is wrong. 9 players would play at 3 tables. It's CEILING(N,4) and the number of tables with 3 players is MOD(4-MOD(N,4),4).
Or I am wrong :)

Regards,
Bernd

PS: Nice to meet you again!
 
Upvote 0
Bernd,

Hmm, aren't you kind... you don't want that last person sitting at the table all by himself.⁉⁉ Yes, of course you are correct... I did not think that one through quite enough.☹ Thanks for catching it.
 
Upvote 0
Thanks for your answers, Rick. It gave me some ideas and found a good way to do it.
If there are 4 chairs per table and we know how many total players there are, the number of tables is know.

To distribute the players at those tables to keep team members apart, have the players line up by teams. All of Team A, followed by all of Team B, etc.
The first person in line sits at the first table, the second the second, etc until we reach the end of the tables and then loop back to Table 1.
This seperates all the team members from each other.

To randomize that process, randomize the order of the teams and the order of the players within each team.

VBA Code:
Sub test()
    Dim rngData As Range, oneCell As Range
    Dim arrTeams() As String, arrPlayers() As String
    Dim i As Long, j As Long, randIndex As Long, temp As Variant
  
    Dim Pointer As Long, playerPointer As Long, teamStartPointer As Long
    Dim PlayersCount As Long
    Dim Tables() As String
    Dim TablesCount As Long, ChairsPerTable As Long
    Dim tableIndex As Long, ChairIndex As Long
    ChairsPerTable = 4
  
    With Sheet1.Range("A:A")
        Set rngData = Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp))
    End With
  
    PlayersCount = rngData.Rows.Count
    ReDim arrTeams(1 To PlayersCount)
    ReDim arrPlayers(1 To PlayersCount)
  
    Rem make array of unique team names
    Pointer = 0
    For Each oneCell In rngData
        If IsError(Application.Match(oneCell.Offset(0, 1).Value, arrTeams, 0)) Then
            Pointer = Pointer + 1
            arrTeams(Pointer) = oneCell.Offset(0, 1).Value
        End If
    Next oneCell
    ReDim Preserve arrTeams(1 To Pointer)
  
    Rem rand reorder teams
    For i = 1 To Pointer
        randIndex = Int(Rnd() * Pointer) + 1
        temp = arrTeams(randIndex)
        arrTeams(randIndex) = arrTeams(i)
        arrTeams(i) = temp
    Next i
  
    Rem line the players up, all team members together
    playerPointer = 0
    For i = 1 To Pointer

        Rem add players from one team
        teamStartPointer = 0
        For Each oneCell In rngData
            With oneCell
                If oneCell.Offset(0, 1).Value = arrTeams(i) Then
                    playerPointer = playerPointer + 1
                    If teamStartPointer = 0 Then teamStartPointer = playerPointer
                    arrPlayers(playerPointer) = oneCell.Value
                End If
           End With
        Next oneCell
      
        Rem re-order the players of the new team
        For j = teamStartPointer To playerPointer
            randIndex = Int(Rnd() * (playerPointer - teamStartPointer + 1)) + teamStartPointer
            temp = arrPlayers(randIndex)
            arrPlayers(randIndex) = arrPlayers(i)
            arrPlayers(i) = temp
        Next j
    Next i
  
   Rem distribute among the tables
    TablesCount = Application.RoundUp(PlayersCount / ChairsPerTable, 0)
    ReDim Tables(1 To TablesCount, 1 To ChairsPerTable)
    tableIndex = 1: ChairIndex = 1
  
    For i = 1 To PlayersCount
        Tables(tableIndex, ChairIndex) = arrPlayers(i)
        tableIndex = tableIndex + 1
        If TablesCount < tableIndex Then
            tableIndex = 1
            ChairIndex = ChairIndex + 1
        End If
    Next i
  
    Rem output results
    With Range("G:G")
        For i = 1 To TablesCount
            With .Cells(Rows.Count, 1).End(xlUp).Offset(2, 0)
                For j = 1 To ChairsPerTable
                    .Cells(1, j).Value = Tables(i, j)
                Next j
            End With
        Next i
    End With
End Sub

Wow, Mike, just WOW! I really really appreciate all the work, you put into this. Now it's up to me, to get my stuff together and figure out, how it works and what needs to be edited.
Thanks a lot!
 
Upvote 0
Thanks for everyone contributing to this. In the meantime, I found a way to randomize the seating order but will surely implement Mike's code, later on.

You guys are awesome,
Lena
 
Upvote 0
If you sort the range with team as the first field and the RAND helper column as the second, that will ranodmize the players, but keep the teams together.
Putting =INDEX($N:$N, 1+4*(ROW(A1)-1)+COLUMN(A1),1) in a cell and dragging right and down to fill a nx4 grid will assign players to tables.
 
Upvote 0

Forum statistics

Threads
1,214,817
Messages
6,121,717
Members
449,050
Latest member
MiguekHeka

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