Public Sub GenerateSchedule()
Dim wsP As Worksheet, wsS As Worksheet, wsM As Worksheet
Set wsP = Sheets("Spelers")
Set wsS = Sheets("Speelschema")
Set wsM = Sheets("Matrix")
Dim n As Long: n = 30
Dim i As Long, r As Long, w As Long
Dim rowOut As Long: rowOut = 3
' Spelersnamen
Dim naam(1 To 30) As String
For i = 1 To n
naam(i) = wsP.Cells(i + 1, 2).Value
If naam(i) = "" Then naam(i) = "Speler " & i
Next i
' Startopstelling
Dim arr(1 To 30) As Long
For i = 1 To n: arr(i) = i: Next i
wsS.Rows("3:2000").ClearContents
wsM.Range("B2:AE31").ClearContents
' =========================
' HEENRONDE (1–29)
' =========================
For r = 1 To n - 1
For w = 1 To n \ 2
Dim thuis As Long, uit As Long
thuis = arr(w)
uit = arr(n - w + 1)
wsS.Cells(rowOut, 1) = r
wsS.Cells(rowOut, 3) = w
wsS.Cells(rowOut, 4) = naam(thuis)
wsS.Cells(rowOut, 5) = Application.Index(Blad1.Range("B1:C31"), Application.Match(naam(thuis), Blad1.Columns(2), 0), 2)
wsS.Cells(rowOut, 6) = naam(uit)
wsS.Cells(rowOut, 7) = Application.Index(Blad1.Range("B1:C31"), Application.Match(naam(uit), Blad1.Columns(2), 0), 2)
wsM.Cells(thuis + 1, uit + 1) = r
rowOut = rowOut + 1
Next w
Call Rotate(arr)
Next r
' =========================
' TERUGRONDE (30–58)
' =========================
For r = 1 To n - 1
For w = 1 To n \ 2
Dim thuis2 As Long, uit2 As Long
thuis2 = arr(n - w + 1)
uit2 = arr(w)
wsS.Cells(rowOut, 1) = r + (n - 1)
wsS.Cells(rowOut, 3) = w
wsS.Cells(rowOut, 4) = naam(thuis2)
wsS.Cells(rowOut, 5) = naam(uit2)
wsM.Cells(thuis2 + 1, uit2 + 1) = r + (n - 1)
rowOut = rowOut + 1
Next w
Call Rotate(arr)
Next r
MsgBox "Heen- en terugronde gegenereerd (thuis/uit).", vbInformation
End Sub