1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| Sub TruckSoluce()
Dim tbopass() As Variant
Dim I As Long, J As Long, K As Long
Dim rezultat() As Variant
Dim tboCamions() As Variant
Dim blnExiste As Boolean
Dim Chrono1 As Single, Chrono2 As Single
Chrono1 = Timer
Worksheets(2).Select
tbopass = Range("A2:H38950")
'*** liste camions ****
ReDim Preserve tboCamions(1) 'sera l'Array de notre liste de camions
For I = 1 To UBound(tbopass, 1) ' on boucle sur les lignes
For J = 1 To UBound(tboCamions) ' on boucle sur le nombre de camions (progressif)
If tbopass(I, 6) = tboCamions(J) Then blnExiste = True 'si le camion est dans la liste, on met notre booleen à true
Next J
If blnExiste = False Then 'si le camion n'existe pas dans la liste
If tboCamions(1) <> "" Then ReDim Preserve tboCamions(UBound(tboCamions) + 1) 'on lui aloue une place
tboCamions(UBound(tboCamions)) = tbopass(I, 6) 'et on l'ajoute
End If
blnExiste = False 'on remet à false :)
Next I
For I = 1 To UBound(tboCamions) 'Inutile, pour visualiser la liste si besoin...
Cells(I, 10) = tboCamions(I)
Next I
'*** fin liste ***
'*** totaux consos et Km mensuels camions ***
ReDim rezultat(UBound(tboCamions), 25) '25 = 1 col = nom camion et 12 * col conso et Km
For J = 1 To UBound(tboCamions) 'pour chaque camions
For K = 1 To 12 ' pour chaque mois
For I = 1 To UBound(tbopass, 1) 'pour chaque cellule
If tbopass(I, 6) = tboCamions(J) Then ' si cell = camion
If tbopass(I, 5) = K Then 'si cell = mois
rezultat(J, 1) = tboCamions(J) 'camion
rezultat(J, K * 2) = rezultat(J, K * 2) + tbopass(I, 2) 'conso mois
rezultat(J, (K * 2) + 1) = rezultat(J, (K * 2) + 1) + tbopass(I, 8) 'Km mois
End If
End If
Next I
Next K
Next J
Worksheets("feuil3").Range("A2:Y106") = rezultat() ' pour la visualisation si besoin
Chrono2 = Timer
MsgBox Chrono2 - Chrono1 & " secondes" ':)
End Sub |
Partager