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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| Sub Regroupe_par_compte()
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
End With
Dim T#
T = Timer
Set Base = Sheets("feuil1")
Set Resultat = Sheets("feuil2")
Dim d1 As New Dictionary
tblbd = Base.Range("A6:G11839")
Resultat.Range("A6:G20000").Clear
' Utilisation d'un tableau pour stocker les données
Dim data() As Variant
data = tblbd
' Utilisation d'un dictionnaire pour stocker les résultats intermédiaires
Dim d2 As New Dictionary
For i = 1 To UBound(data)
d1(data(i, 5)) = d1(data(i, 5)) + data(i, 7)
If Not d2.Exists(data(i, 5)) Then
d2(data(i, 5)) = i
End If
Next i
j = 6
' Variable pour stocker le grand total
Dim grandTotal As Double
grandTotal = 0
For Each Eleme In d1.Keys
' Affichage de toutes les lignes faisant partie du même compte
For i = 1 To UBound(data)
If data(i, 5) = Eleme Then
Resultat.Cells(j, 1) = data(i, 1)
Resultat.Cells(j, 2) = data(i, 2)
Resultat.Cells(j, 3) = data(i, 3)
Resultat.Cells(j, 4) = data(i, 4)
Resultat.Cells(j, 5) = data(i, 5)
Resultat.Cells(j, 6) = data(i, 6)
Resultat.Cells(j, 7) = data(i, 7)
j = j + 1
End If
Next i
Resultat.Cells(j, 1) = "Total Du Compte " & Eleme
Resultat.Cells(j, 7) = d1.Item(Eleme)
Resultat.Range("A" & j & ":G" & j).Interior.Color = 65535
Resultat.Range("A" & j & ":G" & j).Font.Bold = True
' Mise à jour du grand total
grandTotal = grandTotal + d1.Item(Eleme)
j = j + 1
Next Eleme
' Affichage du grand total à la dernière ligne
Resultat.Cells(j, 1) = "Grand Total"
Resultat.Cells(j, 7) = grandTotal
Resultat.Range("A" & j & ":G" & j).Interior.ColorIndex = xlNone
Resultat.Range("A" & j & ":G" & j).Font.Bold = True
Resultat.Columns("G:G").NumberFormat = "#,##0.00"
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
.DisplayAlerts = True
End With
MsgBox "Durée " & Format(Timer - T, "0.00 \sec"), , "Import"
End Sub |
Partager