
' ======================================================================
' =                                                             TIRAGE DES EQUIPES                                                   =
' ======================================================================
' Tirage 1ere Partie "Triplettes"
' ---------------------------------------------------------------------------------------------------------------------------
Sub Tirage(equip As Integer)
Dim v As Integer, equipe As Integer
Dim i As Integer, F As Integer, H As Integer, col As Integer, joueur As Integer
Dim N&
Dim nom$, prénom$
Dim DicoH As New Scripting.Dictionary
Dim DicoF As New Scripting.Dictionary
Dim plage As Range, cel As Range
Dim Alea As Double
Dim X As Integer
        
Application.ScreenUpdating = False

With Worksheets("Inscriptions")

    Dl = .Cells(Rows.Count, "O").End(xlUp).Row + 1
    .Range("O2:Q" & Dl).ClearContents
    
    If Application.CountIf(.Range("D:D"), "H") + Application.CountIf(.Range("D:D"), "F") <> Application.CountA(.Range("A:A")) - 1 Then
        MsgBox "Vous devez inscrire l'information (sexe) pour tous les joueurs"
        Exit Sub
    End If

    'liste homme et femme numéroté --> colonne H
    For i = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
    
        If .Range("D" & i) = "H" Then .Range("H" & i) = "H" & Application.CountIf(.Range("D2:D" & i), "H")
        If .Range("D" & i) = "F" Then .Range("H" & i) = "F" & Application.CountIf(.Range("D2:D" & i), "F")
    
    Next i
        
        
    H = Evaluate("COUNTIF(" & .Name & "!D:D,""H"")") 'h = la valeur de la cellule en ligne 2 et colonne 10 de la feuille inscription
    F = Evaluate("COUNTIF(" & .Name & "!D:D,""F"")") 'f = la valeur de la cellule en ligne 3 et colonne 10 de la feuille Sh2
    
    equipe = Application.Ceiling((H + F) / equip, 1)
    col = 15 'Attribue une valeur a la variable "col", numéro de colonne (col O) où s'inscrit le résultat
    N = 1 'Attribue une valeur a la variable "n"
        
    'homme
    Do Until DicoH.Count = H 'Faire jusqu'a ce que le dico soit rempli au compte de H

        v = Int((H) * Rnd() + 1)

        If Not DicoH.Exists(v) Then ' vérifier que le tirage au sort n'est pas dans le dico

            DicoH.Add v, "" ' le nombre n'est pas dans le dico alors ajout du nombre
            N = N + 1 ' changement de ligne pour inscription nom et prénom

            joueur = Application.Match("H" & v, .Range("H:H"), 0) 'trouve la ligne correspondance dans la colonne H
            nom = Application.Index(.Range("B:B"), joueur) 'nom (colonne B) de la ligne correspondance
            prénom = Application.Index(.Range("C:C"), joueur) 'prénom (colonne C) de la ligne correspondance
            .Cells(N, col) = nom & "-" & prénom ' inscrit Nom et prénom en colonne O (O=15)

            If N = equipe + 1 Then col = col + 1: N = 1 ' si le nombre de "premier" joueurs pour chaque équipe est atteint on passe a la colonne suivante (P)

        End If
            
    Loop

    If N = equipe + 1 Then col = col + 1 ' on passe a la colonne suivante (Q) pour la prochaine boucle Femme

    'femme
    Do Until DicoF.Count = F

        v = Int((F) * Rnd() + 1)

        If Not DicoF.Exists(v) Then

            DicoF.Add v, ""

            joueur = Application.Match("F" & v, .Range("H:H"), 0)
            nom = Application.Index(.Range("B:B"), joueur)
            prénom = Application.Index(.Range("C:C"), joueur)
            .Cells(N + 1, col) = nom & "-" & prénom
            N = N + 1
            If N = equipe + 1 Then col = col + 1: N = 1

        End If
    Loop
        
    '#### randomize ############################################
    For X = 1 To 3

        Dl = .Cells(Rows.Count, "O").End(xlUp).Row
        
        Set plage = .Range(.Cells(2, "N"), .Cells(Dl, "N"))
        
        plage.Value = ""
        
        If plage.Count > Dl Then Exit Sub
        
        Randomize
        
        For Each cel In plage
        
1               Alea = WorksheetFunction.RandBetween(1, Dl - 1)
            If Application.CountIf(plage, Alea) Then GoTo 1 Else cel = Alea
                
        Next cel
    
        'le tri
        With .Sort
            .SortFields.Clear
            .SortFields.Add Key:=Range("N2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            If X = 1 Then .SetRange Range("N2:O" & Dl)
            If X = 2 Then .SetRange Range("N2:P" & Dl)
            If X = 3 Then .SetRange Range("N2:Q" & Dl)
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Next X
End With
        
    Application.ScreenUpdating = True
        
End Sub
