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
| Option Explicit
Sub sommer_si()
Dim Derlig As Integer
Dim T_colA, T_colC
Dim Dico As Object, Cptr As Integer, Nbre_uniq As Integer, ref
Dim T_uniq, T_out
Dim Cptr_u As Integer, Nbre_lig As Integer, Nbre As Integer, Somme As Double
With Sheets(1)
Derlig = .Columns(1).Find("*", , , , , xlPrevious).Row
'variables tableaux source
T_colA = Application.Transpose(.Range("A2:A" & Derlig).Value)
T_colC = Application.Transpose(.Range("C2:C" & Derlig).Value)
'liste des uniques
Set Dico = CreateObject("scripting.dictionary")
For Cptr = 2 To UBound(T_colA)
ref = T_colA(Cptr)
If Not Dico.exists(ref) Then
Dico.Add ref, 0
End If
Next
Nbre_uniq = Dico.Count
T_uniq = Dico.keys
ReDim T_out(Nbre_uniq - 1)
For Cptr_u = 0 To UBound(T_uniq)
'nombre de lignes ayant la valeur de T_uniq
Nbre_lig = UBound(Filter(T_colA, T_uniq(Cptr_u), True)) + 1
Somme = 0
For Cptr = 1 To UBound(T_colA)
If Nbre = Nbre_lig Then Exit For 'boucle que sur le nombre de ligne de l'unique en cours
If T_uniq(Cptr_u) = T_colA(Cptr) Then
Somme = Somme + T_colC(Cptr)
Nbre = Nbre + 1
End If
Next Cptr
T_out(Cptr_u) = Somme
Next Cptr_u
End With
With Sheets(2)
.Range("A2").Resize(Nbre_uniq, 1) = Application.Transpose(T_uniq)
.Range("B2").Resize(Nbre_uniq, 1) = Application.Transpose(T_out)
.Activate
End With
End Sub |
Partager