Private Sub CommandButton1_Click()
Dim tp#, cible#, t1#, t2#, t3#, t4#, t5#
Dim m1 As Byte, m2 As Byte, m3 As Byte, m4 As Byte, m5 As Byte
Dim i&, j&, k&, p&, q&, m#, maxi#, tablo() As Byte, n&, mes$
tp = Timer
cible = [B1]
t1 = [A4]: t2 = [A5]: t3 = [A6]: t4 = [A7]: t5 = [A8]
m1 = Application.Min(50, Int(CDec(cible / t1)))
m2 = Application.Min(50, Int(CDec(cible / t2)))
m3 = Application.Min(50, Int(CDec(cible / t3)))
m4 = Application.Min(50, Int(CDec(cible / t4)))
m5 = Application.Min(50, Int(CDec(cible / t5)))
'---Détermination du maximum possible---
For i = 1 To m1
For j = 1 To m2
For k = 1 To m3
For p = 1 To m4
For q = 1 To m5
m = i * t1 + j * t2 + k * t3 + p * t4 + q * t5
If m <= cible Then maxi = IIf(m > maxi, m, maxi)
If maxi = cible Then GoTo 1
Next
Next
Next
Next
Next
'---Détermination des combinaisons optima---
1 For i = 1 To m1
For j = 1 To m2
For k = 1 To m3
For p = 1 To m4
For q = 1 To m5
m = i * t1 + j * t2 + k * t3 + p * t4 + q * t5
If m = maxi Then
ReDim Preserve tablo(5, n)
tablo(0, n) = i: tablo(1, n) = j: tablo(2, n) = k: tablo(3, n) = p: tablo(4, n) = q
n = n + 1
End If
Next
Next
Next
Next
Next
'---Stockage ou affichage---
[C5:H65536].ClearContents
mes = Chr(10) & "Durée du calcul : " & Int(Timer - tp) & " s" & Chr(10) & Chr(10) & _
"Maximum atteint : " & Int(CDec(24 * maxi)) & ":" & Format(1440 * maxi - 60 * Int(CDec(24 * maxi)), "00") & Chr(10) & Chr(10) & _
"Nombre de combinaisons : " & n & Chr(10)
If n > 65000 Then
ThisWorkbook.Names.Add "Combinaisons", Application.Transpose(tablo)
MsgBox mes & Chr(10) & "Valeurs stockées dans le nom 'Combinaisons'..." & Chr(10)
Else
If n Then
[C5:G5].Resize(n) = Application.Transpose(tablo)
[H5].Resize(n).FormulaR1C1 = "=RC3*R4C1+RC4*R5C1+RC5*R6C1+RC6*R7C1+RC7*R8C1" 'vérification
End If
MsgBox mes
End If
End Sub