Private Sub actualiser_Click()
'On Error Resume Next
Dim chemin As String: Dim adresse As String
Dim requeteHttp As Object: Dim flux As Object

chemin = ThisWorkbook.Path & "\tirages\loto.zip"
adresse = "https://media.fdj.fr/static/csv/loto/loto_201911.zip"
Set requeteHttp = CreateObject("Microsoft.XMLHTTP")
requeteHttp.Open "GET", adresse
requeteHttp.Send

If requeteHttp.Status = 200 Then
    Set flux = CreateObject("ADODB.Stream")
    flux.Open
    flux.Type = 1
    flux.Write requeteHttp.responseBody
    flux.SaveToFile chemin, 2
    flux.Close
    decompresser chemin, ThisWorkbook.Path & "\tirages\"
    importer Replace(chemin, ".zip", ".csv")
Else
    MsgBox "Une erreur est survenue"
End If

Set requeteHttp = Nothing
Set flux = Nothing

End Sub

Sub decompresser(cheminFichier As String, dossier As String)
Dim source As FolderItems
Dim commande As Shell
Dim objetFichier As Object: Dim leDossier As Object: Dim chaqueFichier As Object

Set objetFichier = CreateObject("scripting.filesystemobject")
Set leDossier = objetFichier.GetFolder(dossier)

If Dir(dossier & "loto.csv") <> "" Then
    Kill dossier & "loto.csv"
End If

Set commande = CreateObject("Shell.Application")
Set source = commande.Namespace(cheminFichier).items

commande.Namespace(dossier).CopyHere source

For Each chaqueFichier In leDossier.Files
    If (Right(chaqueFichier, 4) = ".csv") Then
        Name dossier & chaqueFichier.Name As dossier & "loto.csv"
    End If
Next chaqueFichier

Set commande = Nothing
Set source = Nothing
Set objetFichier = Nothing
Set leDossier = Nothing
Set chaqueFichier = Nothing

End Sub

Sub importer(Fichier As String)
On Error Resume Next
Dim ligne As Integer: Dim compteur As Integer: Dim i As Byte
Dim texte As String: Dim elements As Variant

ligne = 4: compteur = 1

Open Fichier For Input As #1
    Do While Not EOF(1)
        Line Input #1, texte
        texte = Replace(texte, Chr(10), Chr(13))
    Loop
Close #1

Open Fichier For Output As #1
    Print #1, texte
Close #1

Open Fichier For Input As #1
    Do While Not EOF(1)
    Line Input #1, texte
        If compteur > 1 Then
            elements = Split(texte, ";")
            Cells(ligne, 2).Value = DateValue(Format(elements(2), "dd/mm/yyyy"))
            For i = 3 To 8 'Correspondance - décalage une colonne
                Cells(ligne, i).Value = elements(i + 1)
            Next i
            ligne = ligne + 1
        End If
'        MsgBox texte
'        If compteur > 5 Then Exit Do
        compteur = compteur + 1
    Loop
Close #1

End Sub

Private Sub stats_Click()
Dim tabB As Variant: Dim tabC As Variant
Dim ligne As Integer: Dim colonne As Byte
Dim boule As Byte: Dim chance As Byte

ReDim tabB(1 To 49): ReDim tabC(1 To 10)
ligne = 4

Do While Cells(ligne, 3).Value <> ""
  For colonne = 3 To 7
    boule = Cells(ligne, colonne).Value
    tabB(boule) = tabB(boule) + 1
  Next colonne
    chance = Cells(ligne, 8).Value
    tabC(chance) = tabC(chance) + 1
  ligne = ligne + 1
  
Loop

For ligne = 4 To 52
    Sheets("frequences").Cells(ligne, 4).Value = ligne - 3
    Sheets("frequences").Cells(ligne, 5).Value = tabB(ligne - 3)
    If (ligne < 14) Then
    Sheets("frequences").Cells(ligne, 7).Value = ligne - 3
    Sheets("frequences").Cells(ligne, 8).Value = tabC(ligne - 3)
    End If
  Next ligne
  
    Sheets("frequences").Sort.SortFields.Clear
    Sheets("frequences").Sort.SortFields.Add2 Key:=Range("E3:E52")
  With Sheets("frequences").Sort
    .SetRange Range("C3:E52")
    .Header = xlYes
    .Orientation = xlTopToBottom                          'Orientation croissante
    .Apply
  End With
  
    Sheets("frequences").Sort.SortFields.Clear
    Sheets("frequences").Sort.SortFields.Add2 Key:=Range("H3:H13")
  With Sheets("frequences").Sort
    .SetRange Range("G3:H13")
    .Header = xlYes
    .Orientation = xlTopToBottom                          'Orientation croissante
    .Apply
  End With
    
    Sheets("frequences").Activate
    Sheets("frequences").Range("A1").Select
    
    Set tabB = Nothing
    Set tabC = Nothing

End Sub
