help with combinations

joeloveszoe

Board Regular
Joined
Apr 24, 2014
Messages
110
Office Version
  1. 365
Platform
  1. MacOS
hi~
i wonder if i can use excel for a pickleball league game generator

i have 9 players 2 courts

is there a formula i can use to generate games
2 games at a time - 4 player per court - 1 person sitting out

i want at least 10 -12 combinations
each game different partners / opponents and everyone gets a turn to sit out

for example
game 1
court A
player 1 player 2 vs player 3 player 4
court B
player 5 player 6 vs player 7 player 8

player 9 sits out

game 2
player 1 player 8 vs player 7 player 6
court B
player 2 player 3 vs player 4 player 9

player 5 sits out

thanks in advance for your help =)
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
try this

Book1
ABCDEFGHIJK
1GameCourt1Court2Sitting OutPlayers
2John
3Game: 1EmilyDanielMaryMary
4ChristopherSarahDavid
5DavidMichaelSarah
6JohnJessicaMichael
7Emily
8Game: 2JessicaSarahDavidDaniel
9JohnMaryJessica
10DanielChristopherChristopher
11EmilyMichael
12
13Game: 3DanielJohnSarah
14MichaelEmily
15JessicaDavid
16ChristopherMary
17
18Game: 4DanielJohnMichael
19ChristopherDavid
20SarahEmily
21MaryJessica
22
23Game: 5SarahDavidEmily
24JohnChristopher
25JessicaMary
26DanielMichael
27
28Game: 6MichaelDavidDaniel
29JessicaChristopher
30SarahJohn
31EmilyMary
32
33Game: 7DavidEmilyJessica
34MaryDaniel
35JohnChristopher
36MichaelSarah
37
38Game: 8SarahEmilyJohn
39JessicaDavid
40DanielChristopher
41MichaelMary
42
43Game: 9DanielJohnMary
44DavidChristopher
45JessicaEmily
46MichaelSarah
47
48Game: 10SarahDanielDavid
49MichaelMary
50JessicaChristopher
51JohnEmily
52
53Game: 11ChristopherMichaelSarah
54JohnJessica
55MaryEmily
56DavidDaniel
57
58Game: 12MaryDanielMichael
59JohnEmily
60DavidJessica
61ChristopherSarah
62
63Game: 13MichaelDavidEmily
64JessicaMary
65DanielSarah
66ChristopherJohn
67
68Game: 14JessicaJohnDaniel
69SarahChristopher
70MaryMichael
71DavidEmily
72
73Game: 15EmilyChristopherJessica
74JohnDaniel
75DavidSarah
76MaryMichael
77
78
Sheet1


VBA Code:
Option Explicit

Public Sub GenerateSchedule()

    Dim ws As Worksheet
    Dim ar As Variant, playerAr As Variant
    Dim i As Integer, x As Integer, rowStart As Integer, sitOutIndex As Integer
    Dim numGames As Variant, lastRow As Long
    Dim rng As Range

    Set ws = ThisWorkbook.Sheets("Sheet1") ' change to sheetname
    
    numGames = InputBox("Enter the number of games:", "Number of Games")
    
    If Not IsNumeric(numGames) Or numGames <= 0 Then
        MsgBox "Please enter a valid number for the number of games.", vbExclamation
        Exit Sub
    End If

    lastRow = ws.Cells(ws.Rows.Count, "H").End(xlUp).Row
    ReDim ar(0 To lastRow - 2)
    For i = 2 To lastRow
        ar(i - 2) = ws.Cells(i, "H").Value
    Next

    ws.Range("A:D").ClearContents
    rowStart = 3
    sitOutIndex = 0
    
    ws.Range("A1:D1").Value = Array("Game", "Court1", "Court2", "Sitting Out")
    
    For x = 0 To numGames - 1
        playerAr = ar
        sitOutIndex = GetSitOutIndex(ar, sitOutIndex)
        playerAr = RemoveElementFromArray(playerAr, sitOutIndex)
        playerAr = ShuffleArray(playerAr)
        
        For i = 0 To UBound(playerAr)

            If i = 0 Then
                ws.Cells(rowStart, 4).Value = ar(sitOutIndex)
                ws.Cells(rowStart, 1).Value = "Game: " & x + 1
            End If
            
            If i Mod 2 = 0 Then
                ws.Cells(rowStart, 2).Value = playerAr(i)
            Else
                ws.Cells(rowStart, 3).Value = playerAr(i)
                rowStart = rowStart + 1
            End If
        Next
        
        rowStart = rowStart + 1
    Next
End Sub

Function GetSitOutIndex(arr As Variant, currentIndex As Integer) As Integer
    If currentIndex + 1 = UBound(arr) Then
        GetSitOutIndex = 0
    Else
        GetSitOutIndex = currentIndex + 1
    End If
End Function

Function RemoveElementFromArray(arr As Variant, index As Integer) As Variant
    Dim i As Integer
    Dim newArr As Variant
    ReDim newArr(UBound(arr) - 1)
    
    For i = 0 To UBound(newArr)
        If i < index Then
            newArr(i) = arr(i)
        Else
            newArr(i) = arr(i + 1)
        End If
    Next
    RemoveElementFromArray = newArr
End Function

Function ShuffleArray(arr As Variant) As Variant
    Dim i As Integer
    Dim j As Integer
    Dim temp As Variant
    
    Randomize
    For i = UBound(arr) To LBound(arr) + 1 Step -1
        j = Int((i - 1 + 1) * Rnd + LBound(arr))
        temp = arr(j)
        arr(j) = arr(i)
        arr(i) = temp
    Next
    ShuffleArray = arr
End Function
 
Upvote 1
try this

Book1
ABCDEFGHIJK
1GameCourt1Court2Sitting OutPlayers
2John
3Game: 1EmilyDanielMaryMary
4ChristopherSarahDavid
5DavidMichaelSarah
6JohnJessicaMichael
7Emily
8Game: 2JessicaSarahDavidDaniel
9JohnMaryJessica
10DanielChristopherChristopher
11EmilyMichael
12
13Game: 3DanielJohnSarah
14MichaelEmily
15JessicaDavid
16ChristopherMary
17
18Game: 4DanielJohnMichael
19ChristopherDavid
20SarahEmily
21MaryJessica
22
23Game: 5SarahDavidEmily
24JohnChristopher
25JessicaMary
26DanielMichael
27
28Game: 6MichaelDavidDaniel
29JessicaChristopher
30SarahJohn
31EmilyMary
32
33Game: 7DavidEmilyJessica
34MaryDaniel
35JohnChristopher
36MichaelSarah
37
38Game: 8SarahEmilyJohn
39JessicaDavid
40DanielChristopher
41MichaelMary
42
43Game: 9DanielJohnMary
44DavidChristopher
45JessicaEmily
46MichaelSarah
47
48Game: 10SarahDanielDavid
49MichaelMary
50JessicaChristopher
51JohnEmily
52
53Game: 11ChristopherMichaelSarah
54JohnJessica
55MaryEmily
56DavidDaniel
57
58Game: 12MaryDanielMichael
59JohnEmily
60DavidJessica
61ChristopherSarah
62
63Game: 13MichaelDavidEmily
64JessicaMary
65DanielSarah
66ChristopherJohn
67
68Game: 14JessicaJohnDaniel
69SarahChristopher
70MaryMichael
71DavidEmily
72
73Game: 15EmilyChristopherJessica
74JohnDaniel
75DavidSarah
76MaryMichael
77
78
Sheet1


VBA Code:
Option Explicit

Public Sub GenerateSchedule()

    Dim ws As Worksheet
    Dim ar As Variant, playerAr As Variant
    Dim i As Integer, x As Integer, rowStart As Integer, sitOutIndex As Integer
    Dim numGames As Variant, lastRow As Long
    Dim rng As Range

    Set ws = ThisWorkbook.Sheets("Sheet1") ' change to sheetname
   
    numGames = InputBox("Enter the number of games:", "Number of Games")
   
    If Not IsNumeric(numGames) Or numGames <= 0 Then
        MsgBox "Please enter a valid number for the number of games.", vbExclamation
        Exit Sub
    End If

    lastRow = ws.Cells(ws.Rows.Count, "H").End(xlUp).Row
    ReDim ar(0 To lastRow - 2)
    For i = 2 To lastRow
        ar(i - 2) = ws.Cells(i, "H").Value
    Next

    ws.Range("A:D").ClearContents
    rowStart = 3
    sitOutIndex = 0
   
    ws.Range("A1:D1").Value = Array("Game", "Court1", "Court2", "Sitting Out")
   
    For x = 0 To numGames - 1
        playerAr = ar
        sitOutIndex = GetSitOutIndex(ar, sitOutIndex)
        playerAr = RemoveElementFromArray(playerAr, sitOutIndex)
        playerAr = ShuffleArray(playerAr)
       
        For i = 0 To UBound(playerAr)

            If i = 0 Then
                ws.Cells(rowStart, 4).Value = ar(sitOutIndex)
                ws.Cells(rowStart, 1).Value = "Game: " & x + 1
            End If
           
            If i Mod 2 = 0 Then
                ws.Cells(rowStart, 2).Value = playerAr(i)
            Else
                ws.Cells(rowStart, 3).Value = playerAr(i)
                rowStart = rowStart + 1
            End If
        Next
       
        rowStart = rowStart + 1
    Next
End Sub

Function GetSitOutIndex(arr As Variant, currentIndex As Integer) As Integer
    If currentIndex + 1 = UBound(arr) Then
        GetSitOutIndex = 0
    Else
        GetSitOutIndex = currentIndex + 1
    End If
End Function

Function RemoveElementFromArray(arr As Variant, index As Integer) As Variant
    Dim i As Integer
    Dim newArr As Variant
    ReDim newArr(UBound(arr) - 1)
   
    For i = 0 To UBound(newArr)
        If i < index Then
            newArr(i) = arr(i)
        Else
            newArr(i) = arr(i + 1)
        End If
    Next
    RemoveElementFromArray = newArr
End Function

Function ShuffleArray(arr As Variant) As Variant
    Dim i As Integer
    Dim j As Integer
    Dim temp As Variant
   
    Randomize
    For i = UBound(arr) To LBound(arr) + 1 Step -1
        j = Int((i - 1 + 1) * Rnd + LBound(arr))
        temp = arr(j)
        arr(j) = arr(i)
        arr(i) = temp
    Next
    ShuffleArray = arr
End Function
this looks very well thought out & creative thank you so much for your help!
sadly i will need a crash course on VBA s

happy friday to you!!!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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