Option Explicit
Sub Jad73Q()
Dim Ff%, Ch$, a$, b$(), Ta&(), Tb&(), i&, j&, Tc&(1 To 916895), k As Byte, Fn$(1), n&, Ag$ '916895=nb de combinaisons de 4 parmi 70
Application.Calculation = xlCalculationManual
Fn(0) = "keno_gagnant_a_vie.csv": Fn(1) = "keno.csv" 'nom des fichiers sources, + récent en premier
Ta = CmbN(20, 4) 'liste des combinaisons de 4 parmi 20
ReDim Tb(4) 'quarté en cours à incrémenter (tb(0)=0)
For k = 0 To 1
Ch = ThisWorkbook.Path & "\" & Fn(k) 'sources et classeur, même dossier
LfToCrlf (Ch) 'sources : de LF vers CRLF
If k = 0 Then
Ff = FreeFile
Open Ch For Input As #Ff 'sources: ordre tirage décroissant
Line Input #Ff, a: Line Input #Ff, a: b = Split(a, ";"): Ag = b(0) 'n° dernier tirage (aaaannn) pr future actualisation
Close #Ff
End If
Ff = FreeFile
Open Ch For Input As #Ff
Line Input #Ff, a 'ignorer ligne de titre
Do While Not EOF(Ff)
Line Input #Ff, a
If a <> "" Then 'ignorer éventuels CRLF parasites
b = Split(a, ";")
For i = 1 To UBound(Ta)
For j = 1 To 4
Tb(j) = b(Ta(i, j) + 3) 'sources: liste n° commence col 5
Next j
j = CmbRk(Tb, 70, 4) 'rang du quarté à incrémenter
Tc(j) = Tc(j) + 1 'incrémentation
Next i
n = n + 1 'compteur
Application.StatusBar = "Nb de tirages analysés : " & n
DoEvents
End If
Loop
Close #Ff
Next k
Erase Ta: Erase Tb: Erase b
Sheets.Add
Application.StatusBar = "Affichage..."
Application.ScreenUpdating = False
Cells(1, 3) = n & " tirages"
Cells(2, 3) = "numéro du dernier tirages : " & Ag
For i = 1 To UBound(Tc): Cells(i, 1) = Tc(i): Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.StatusBar = "Ready"
End Sub
Function CmbN(ByVal a&, ByVal b&) As Long() 'combinaisons(a,b)
Dim n&, Tb&(), c&, i&, j&
n = CmbNb(a, b): ReDim Tb(1 To n, 1 To b): c = a - b
For i = 1 To b: Tb(1, i) = i: Next i
For i = 2 To n
If b = 1 Then Tb(i, 1) = Tb(i - 1, 1) + 1 Else Tb(i, 1) = Tb(i - 1, 1) - (Tb(i - 1, 2) = c + 2)
For j = 2 To b - 1
If Tb(i - 1, j + 1) = c + j + 1 Then
If Tb(i - 1, j) = c + j Then Tb(i, j) = Tb(i, j - 1) + 1 Else Tb(i, j) = Tb(i - 1, j) + 1
Else
Tb(i, j) = Tb(i - 1, j)
End If
Next j
If Tb(i - 1, b) = a Then Tb(i, b) = Tb(i, b - 1) + 1 Else Tb(i, b) = Tb(i - 1, b) + 1
Next i
CmbN = Tb
End Function
Function CmbNb(ByVal a&, ByVal b&) As Long 'nb combinaisons(a,b)
Dim c&
c = a - b
If c = 0 Then
CmbNb = 1
Else
If b < c Then c = b
CmbNb = FacL(a, c) / FacL(c)
End If
End Function
Function FacL(ByVal a&, Optional b) As Long 'factorielle (option nb itérations)
Dim i&, c&
If Not IsMissing(b) Then c = CLng(b) Else c = a
If a = 0 Or c = 0 Then FacL = 1: Exit Function
FacL = a
For i = 2 To c: FacL = FacL * (a - i + 1): Next i
End Function
Sub LfToCrlf(Ch$) 'fichier Lf en fichier CrLf
Dim a$, Ff%
Ff = FreeFile
Open Ch For Input As #Ff
a = Replace$(Replace$(Input(LOF(1), #Ff), vbCrLf, vbLf), vbLf, vbCrLf)
Close #Ff
Kill Ch
Ff = FreeFile
Open Ch For Output As #Ff
Print #Ff, a
Close #Ff
End Sub
Function CmbRk(Cb&(), a&, b&) As Long 'rang de la combinaison Cb (cb(0)=0) dans combinaisons(a,b)
Dim i&, j&
For j = 1 To UBound(Cb): For i = Cb(j - 1) + 2 To Cb(j): CmbRk = CmbRk + CmbNb(a + 1 - i, b - j): Next i, j
CmbRk = CmbRk + 1
End Function
Function NCmb(ByVal a&, ByVal b&, ByVal c&) As Long() 'c ième combinaison(a,b)
Dim Tb&(), i&, bS As Boolean, x&, d&
ReDim Tb(0 To b)
Do
d = d + 1: x = 0: bS = False
For i = a - 1 - Tb(d - 1) To b - d Step -1
x = x + CmbNb(i, b - d)
If c <= x Then bS = True: Exit For
Next i
Tb(d) = a - i: c = c - x + CmbNb(i, b - d)
Loop Until d = b
NCmb = Tb
End Function
Function NCmbTxt(ByVal a&, ByVal b&, ByVal c&) As String 'c ième combinaison(a,b), mode texte
Dim d&(), i&
d = NCmb(a, b, c): NCmbTxt = d(1)
For i = 2 To UBound(d): NCmbTxt = NCmbTxt & "," & d(i): Next i
End Function
Sub Jad73QActua()
Dim Ff%, Ch$, a$, b$(), Ta&(), Tb&(), i&, j&, Tc&(1 To 916895), n&, Dt$, c&, d&, e&, Ag$
If MsgBox("Cette sub actualise les résultats de la feuille active", 1) = 2 Then Exit Sub
Do
Dt = Application.InputBox(prompt:="n° du dernier tirage pris en compte ?", Title:="format aaaannn", Type:=2): c = Val(Dt)
If c = 0 Then Exit Sub
Loop Until Len(CStr(c)) = 7
Application.Calculation = xlCalculationManual
c = CLng(Left(Dt, 4)): d = CLng(Right(Dt, 3)): Ch = ThisWorkbook.Path & "\keno_gagnant_a_vie.csv": LfToCrlf (Ch): Ff = FreeFile
Open Ch For Input As #Ff
Line Input #Ff, a: Line Input #Ff, a: b = Split(a, ";"): Ag = b(0)
Close #Ff
Ta = CmbN(20, 4): ReDim Tb(4): Ff = FreeFile
Open Ch For Input As #Ff
Line Input #Ff, a
Do While Not EOF(Ff)
Line Input #Ff, a
If a <> "" Then
b = Split(a, ";"): e = CLng(Left(b(0), 4))
If e < c Or (e = c And Not (CLng(Right(b(0), 3)) > d)) Then Exit Do
For i = 1 To UBound(Ta): For j = 1 To 4
Tb(j) = b(Ta(i, j) + 3)
Next j
j = CmbRk(Tb, 70, 4): Tc(j) = Tc(j) + 1
Next i
n = n + 1: Application.StatusBar = "Nb de tirages analysés : " & n: DoEvents
End If
Loop
Close #Ff
Erase Ta: Erase Tb: Erase b
If n > 0 Then
Application.StatusBar = "Actualisation de l'affichage..."
Application.ScreenUpdating = False
Cells(1, 3) = n & " tirages ajoutés": Cells(2, 3) = "numéro du dernier tirage : " & Ag
For i = 1 To UBound(Tc)
If Tc(i) > 0 Then Cells(i, 1) = Cells(i, 1) + Tc(i)
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Cells(3, 3) = "nombre de tirages total : " & (WorksheetFunction.Sum(Range(Cells(1, 1), Cells(916895, 1))) / WorksheetFunction.Combin(20, 4))
Application.Calculation = xlCalculationManual
End If
Application.Calculation = xlCalculationAutomatic
Application.StatusBar = "Ready"
End Sub